functions/ConvertFrom-JsonWithArgs.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

InModuleScope ImprovedConvertFromJson {

    #region Test data
    $jsonHash = @"
 {
     "glossary": {
         "title": "example glossary",
   "GlossDiv": {
             "title": "S",
    "GlossList": {
                 "GlossEntry": {
                     "ID": "SGML",
      "SortAs": "SGML",
      "GlossTerm": "Standard Generalized Markup Language",
      "Acronym": "SGML",
      "Abbrev": "ISO 8879:1986",
      "GlossDef": {
                         "para": "A meta-markup language, used to create markup languages such as DocBook.",
       "GlossSeeAlso": ["GML", "XML"]
                     },
      "GlossSee": "markup"
                 }
             }
         }
     }
 }
"@


    $jsonArray = @"
[
  {
   color: "red",
   value: "#f00"
  },
  {
   color: "green",
   value: "#0f0"
  },
  {
   color: "blue",
   value: "#00f"
  }
 ]
"@

#endregion

    Describe "ConvertFrom-JsonWithArgs" {
        It "Can parse JSON dictionary" {
            $output = ConvertFrom-JsonWithArgs -InputObject $jsonHash
            $output                     | Should Not BeNullOrEmpty
            $output.glossary             | Should Not BeNullOrEmpty
            $output.glossary.title         | Should Not BeNullOrEmpty
            $output.glossary.title        | Should Be "example glossary"
        }

        It "Can parse JSON array" {
            $output = ConvertFrom-JsonWithArgs -InputObject $jsonArray
            $output                     | Should Not BeNullOrEmpty
            $output.Length                 | Should Be 3
            $output[0]                     | Should Not BeNullOrEmpty
            $output[0].color             | Should Be "red"
        }

        It "Sets MaxLength" {
            {ConvertFrom-JsonWithArgs -InputObject $jsonArray -MaxJsonLength 1} | Should Throw
        }
    }
}