|
| 1 | +Class DeclarativeCOS.BinderTests Extends (%UnitTest.TestCase, DeclarativeCOS.DeclarativeProvider) |
| 2 | +{ |
| 3 | + |
| 4 | +Parameter TESTGLOBALNAME As %String = "^DeclarativeCOSTests"; |
| 5 | + |
| 6 | +/// @Declarative("test.forEach:toGlobal") |
| 7 | +ClassMethod toGlobal(word As %String) |
| 8 | +{ |
| 9 | + set global = $name(@..#TESTGLOBALNAME@("forEach")) |
| 10 | + |
| 11 | + if ('$data(@global@("word1"))) { set @global@("word1") = word } |
| 12 | + |
| 13 | + elseif ('$data(@global@("word2"))) { set @global@("word2") = word } |
| 14 | + |
| 15 | + elseif ('$data(@global@("word3"))) { set @global@("word3") = word } |
| 16 | +} |
| 17 | + |
| 18 | +/// @Declarative("test.map:nameNumber") |
| 19 | +ClassMethod nameNumber(value As %Numeric) As %String |
| 20 | +{ |
| 21 | + return $select(value=1:"one", value=2:"two", value=3:"three", 1:"") |
| 22 | +} |
| 23 | + |
| 24 | +/// @Declarative("test.find:isEven") |
| 25 | +ClassMethod isEven(value As %Numeric) |
| 26 | +{ |
| 27 | + return value # 2 = 0 |
| 28 | +} |
| 29 | + |
| 30 | +/// @Declarative("test.filter:isOdd") |
| 31 | +ClassMethod isOdd(value As %Numeric) |
| 32 | +{ |
| 33 | + return '..isEven(value) |
| 34 | +} |
| 35 | + |
| 36 | +/// @Declarative("test.exists:isNameMaks") |
| 37 | +ClassMethod isNameMaks(value As %String) |
| 38 | +{ |
| 39 | + return value = "Maks" |
| 40 | +} |
| 41 | + |
| 42 | +/// @Declarative("test.exists:isNamePeter") |
| 43 | +ClassMethod isNamePeter(value As %String) |
| 44 | +{ |
| 45 | + return value = "Peter" |
| 46 | +} |
| 47 | + |
| 48 | +/// @Declarative("test.count:greateOrEqualThan2000") |
| 49 | +ClassMethod greateOrEqualThan2000(value As %Numeric) |
| 50 | +{ |
| 51 | + return (value >= 2000) |
| 52 | +} |
| 53 | + |
| 54 | +Method TestForEach() |
| 55 | +{ |
| 56 | + set global = $name(@..#TESTGLOBALNAME@("forEach")) |
| 57 | + |
| 58 | + kill @global |
| 59 | + |
| 60 | + set $lb(word1, word2, word3) = $lb("ForEach", "is", "available") |
| 61 | + |
| 62 | + set words = ##class(%ListOfDataTypes).%New() |
| 63 | + do words.Insert(word1) |
| 64 | + do words.Insert(word2) |
| 65 | + do words.Insert(word3) |
| 66 | + |
| 67 | + zforeach $zbind(words, "test.forEach:toGlobal") |
| 68 | + |
| 69 | + do $$$AssertEquals(word1, $get(@global@("word1")), "Word #1 is equal") |
| 70 | + do $$$AssertEquals(word2, $get(@global@("word2")), "Word #2 is equal") |
| 71 | + do $$$AssertEquals(word3, $get(@global@("word3")), "Word #3 is equal") |
| 72 | + |
| 73 | + kill @global |
| 74 | +} |
| 75 | + |
| 76 | +Method TestMap() |
| 77 | +{ |
| 78 | + set (number1, number2, number3) = $lb(1, 2, 3) |
| 79 | + |
| 80 | + set numbers = ##class(%ListOfDataTypes).%New() |
| 81 | + do numbers.Insert(number1) |
| 82 | + do numbers.Insert(number2) |
| 83 | + do numbers.Insert(number3) |
| 84 | + |
| 85 | + set namedNumbers = $zmap(numbers, "test.map:nameNumber") |
| 86 | + |
| 87 | + do $$$AssertEquals(numbers.Count(), namedNumbers.Count(), "Numbers collections must have the same count of items") |
| 88 | + |
| 89 | + do $$$AssertEquals(numbers.%ClassName(), namedNumbers.%ClassName(), "Numbers collections must have the same class name") |
| 90 | + |
| 91 | + do $$$AssertEquals(..nameNumber(number1), namedNumbers.GetAt(number1), "1 == one") |
| 92 | + do $$$AssertEquals(..nameNumber(number2), namedNumbers.GetAt(number2), "2 == two") |
| 93 | + do $$$AssertEquals(..nameNumber(number3), namedNumbers.GetAt(number3), "3 == three") |
| 94 | +} |
| 95 | + |
| 96 | +Method TestFind() |
| 97 | +{ |
| 98 | + set (number1, number2, number3) = $lb(1, 2, 3) |
| 99 | + |
| 100 | + set numbers = ##class(%ListOfDataTypes).%New() |
| 101 | + do numbers.Insert(number1) |
| 102 | + do numbers.Insert(number2) |
| 103 | + do numbers.Insert(number3) |
| 104 | + |
| 105 | + set evenNumber = $zfind(numbers, "test.find:isEven") |
| 106 | + |
| 107 | + do $$$AssertEquals(number2, evenNumber, "2 is even") |
| 108 | + |
| 109 | + do numbers.RemoveAt(number2) |
| 110 | + |
| 111 | + do $$$AssertNotTrue($zfind(numbers, "test.find:isEven"), "Numbers collection has no even numbers now") |
| 112 | +} |
| 113 | + |
| 114 | +Method TestFilter() |
| 115 | +{ |
| 116 | + set $lb(number1, number2, number3) = $lb(1, 2, 3) |
| 117 | + |
| 118 | + set numbers = ##class(%ListOfDataTypes).%New() |
| 119 | + do numbers.Insert(number1) |
| 120 | + do numbers.Insert(number2) |
| 121 | + do numbers.Insert(number3) |
| 122 | + |
| 123 | + set oddNumbers = $zfilter(numbers, "test.filter:isOdd") |
| 124 | + |
| 125 | + do $$$AssertEquals(numbers.Count() - 1, oddNumbers.Count(), "Numbers collections contains only one even number") |
| 126 | + |
| 127 | + do $$$AssertEquals(numbers.%ClassName(), oddNumbers.%ClassName(), "Numbers collections must have the same class name") |
| 128 | + |
| 129 | + do $$$AssertEquals(number1, oddNumbers.GetAt(1), "1 is odd") |
| 130 | + do $$$AssertEquals(number3, oddNumbers.GetAt(2), "3 is odd") |
| 131 | +} |
| 132 | + |
| 133 | +Method TestExists() |
| 134 | +{ |
| 135 | + set $lb(nameMaks, nameJohn, nameDonny) = $lb("Maks", "John", "Donny") |
| 136 | + |
| 137 | + set names = ##class(%ListOfDataTypes).%New() |
| 138 | + do names.Insert(nameMaks) |
| 139 | + do names.Insert(nameJohn) |
| 140 | + do names.Insert(nameDonny) |
| 141 | + |
| 142 | + set isNameMaksExist = $zexists(names, "test.exists:isNameMaks") |
| 143 | + |
| 144 | + do $$$AssertTrue(isNameMaksExist, """Maks"" exists in names collection") |
| 145 | + |
| 146 | + set isNamePeterExist = $zexists(names, "test.exists:isNamePeter") |
| 147 | + |
| 148 | + do $$$AssertNotTrue(isNamePeterExist, """Peter"" doesn't exists in names collection") |
| 149 | +} |
| 150 | + |
| 151 | +Method TestCount() |
| 152 | +{ |
| 153 | + set $lb(year1998, year1999, year2000, year2001) = $lb(1998, 1999, 2000, 2001) |
| 154 | + |
| 155 | + set years = ##class(%ListOfDataTypes).%New() |
| 156 | + do years.Insert(year1998) |
| 157 | + do years.Insert(year1999) |
| 158 | + do years.Insert(year2000) |
| 159 | + do years.Insert(year2001) |
| 160 | + |
| 161 | + set expectedFilteredYears = ##class(%ListOfDataTypes).%New() |
| 162 | + do expectedFilteredYears.Insert(year2000) |
| 163 | + do expectedFilteredYears.Insert(year2001) |
| 164 | + |
| 165 | + set filteredYearsCount = $zcount(years, "test.count:greateOrEqualThan2000") |
| 166 | + |
| 167 | + do $$$AssertEquals(expectedFilteredYears.Count(), filteredYearsCount, "Only two years should be filtered") |
| 168 | +} |
| 169 | + |
| 170 | +} |
| 171 | + |
0 commit comments