From 1dbf519ae0c66333566736e06435649731d33594 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Sun, 8 Sep 2024 16:57:00 -0300 Subject: [PATCH 1/2] initial nix prelude tests --- Makefile | 1 + test/nix_prelude/.gitignore | 1 + test/nix_prelude/Makefile | 6 +++ test/nix_prelude/main.nix | 95 +++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 test/nix_prelude/.gitignore create mode 100644 test/nix_prelude/Makefile create mode 100644 test/nix_prelude/main.nix diff --git a/Makefile b/Makefile index b5c2ccc19..89a220dce 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ test: ## Run the compiler unit tests cargo clippy cd test/language && make cd test/javascript_prelude && make test + cd test/nix_prelude && make test cd test/project_erlang && cargo run clean && cargo run check && cargo run test cd test/project_javascript && cargo run clean && cargo run check && cargo run test cd test/project_deno && cargo run clean && cargo run check && cargo run test diff --git a/test/nix_prelude/.gitignore b/test/nix_prelude/.gitignore new file mode 100644 index 000000000..7cb66705d --- /dev/null +++ b/test/nix_prelude/.gitignore @@ -0,0 +1 @@ +prelude.nix diff --git a/test/nix_prelude/Makefile b/test/nix_prelude/Makefile new file mode 100644 index 000000000..f8c803e60 --- /dev/null +++ b/test/nix_prelude/Makefile @@ -0,0 +1,6 @@ +.PHONY: test +test: + @echo test/nix_prelude + @cp ../../compiler-core/templates/prelude.nix prelude.nix + @nix-instantiate --show-trace --eval main.nix + @rm prelude.nix diff --git a/test/nix_prelude/main.nix b/test/nix_prelude/main.nix new file mode 100644 index 000000000..6a61b2e66 --- /dev/null +++ b/test/nix_prelude/main.nix @@ -0,0 +1,95 @@ +let + inherit + (import ./prelude.nix) + Ok + Error + isOk + remainderInt + divideInt + divideFloat + toList + prepend + listIsEmpty + listToArray + listHasAtLeastLength + listHasLength + strHasPrefix + parseNumber + parseEscape + UtfCodepoint + stringBits + codepointBits + BitArray + toBitArray + sizedInt + byteSize + byteAt + binaryFromBitSlice + intFromBitSlice + byteArrayToInt + byteArrayToUtf8String + ; + + assertEq = a: b: assert ((a == b) || builtins.throw ["Assertion failed:" a "!=" b]); null; +in builtins.deepSeq { + testRES1 = assert isOk (Ok 5); null; + testRES2 = assert !(isOk (Error 5)); null; + + testAR1 = assertEq (remainderInt 10 0) 0; + testAR2 = assertEq (remainderInt 11 3) 2; + testAR3 = assertEq (remainderInt (-27) 5) (-2); + testAR4 = assertEq (divideInt 11 0) 0; + testAR5 = assertEq (divideInt 121 2) 60; + testAR6 = assertEq (divideFloat 121.5 0.0) 0.0; + testAR7 = assertEq (divideFloat 121.0 2.0) 60.5; + + testLS1 = assertEq (toList [ 1 2 3 4 ]) (prepend 1 (prepend 2 (prepend 3 (prepend 4 (toList []))))); + testLS2 = assertEq (toList [ 1 2 3 4 ]).tail.tail.head 3; + testLS3 = assertEq (listToArray (toList [ 1 2 3 "abc" { a = 5; } ])) [ 1 2 3 "abc" { a = 5; } ]; + testLS4 = assert listIsEmpty (toList [ 1 2 3 4 ]).tail.tail.tail.tail; null; + testLS5 = assert listHasAtLeastLength (toList [ 1 2 3 4 ]) 4; null; + testLS6 = assert listHasAtLeastLength (toList [ 1 2 3 4 ]) 0; null; + testLS7 = assert listHasAtLeastLength (toList [ 1 2 3 4 ]) (-1); null; + testLS8 = assert !(listHasAtLeastLength (toList [ 1 2 3 4 ]) 5); null; + testLS9 = assert listHasAtLeastLength (toList []) 0; null; + testLS10 = assert !(listHasAtLeastLength (toList []) 1); null; + testLS11 = assert listHasLength (toList [ 1 2 3 4 ]) 4; null; + testLS12 = assert !(listHasLength (toList [ 1 2 3 4 ]) 3); null; + testLS13 = assert listHasLength (toList []) 0; null; + testLS14 = assert !(listHasLength (toList []) 1); null; + + testSTR1 = assert strHasPrefix "ab" "abc"; null; + testSTR2 = assert strHasPrefix "" "abc"; null; + testSTR3 = assert !(strHasPrefix "bb" "abc"); null; + + testLT1 = assertEq (parseNumber "0xff") 255; + testLT2 = assertEq (parseNumber "0b1000") 8; + testLT3 = assertEq (parseNumber "0o11") 9; + testLT4 = assertEq (parseNumber "1234") 1234; + testLT5 = assertEq (parseEscape "\\n") "\n"; + + testUTF1 = assertEq (UtfCodepoint 123).value 123; + testUTF2 = + assertEq + (stringBits "héllo [ˈaʳʊ] ℕ ⊆ ℕ₀ ὦ ἄνδρ ⡌ コンニ ░▒▓█") + [ 104 195 169 108 108 111 32 91 203 136 97 202 179 202 138 93 32 226 132 149 32 226 138 134 32 226 132 149 226 130 128 32 225 189 166 32 225 188 132 206 189 206 180 207 129 32 226 161 140 32 227 130 179 227 131 179 227 131 139 32 226 150 145 226 150 146 226 150 147 226 150 136 ] + ; + testUTF3 = assertEq (codepointBits (UtfCodepoint 9608)) [ 226 150 136 ]; + + testBIT1 = assertEq (BitArray [ 1 2 3 ]).buffer [ 1 2 3 ]; + # testBIT2 = assertEq (toBitArray [ 1 (-2) 511 256 255 ]).buffer [ 1 254 255 0 255 ]; + testBIT3 = assertEq (toBitArray [ 1 [ 1 2 255 ] 256 [ ] 24 [ 3 ] [ 4 5 ] ]) (BitArray [ 1 1 2 255 0 24 3 4 5 ]); + testBIT4 = assertEq (sizedInt 8 32) [ 0 0 0 8 ]; + testBIT5 = assertEq (sizedInt 32767 16) [ 127 255 ]; + testBIT6 = assertEq (byteSize (toBitArray [ 1 2 3 4 5 ])) 5; + testBIT7 = assertEq (byteAt (toBitArray [ 1 2 3 4 5 ]) 3) 4; + testBIT8 = assertEq (binaryFromBitSlice (toBitArray [ 1 2 3 4 5 ]) 1 3) (BitArray [ 2 3 ]); + # testBIT9 = assertEq (binaryFromBitSlice (toBitArray [ 1 2 3 4 5 ]) (-99) 99) (BitArray [ 1 2 3 4 5 ]); + # testBIT10 = assertEq (binaryFromBitSlice (toBitArray [ ]) 1 3) (BitArray [ ]); + testBIT11 = assertEq (intFromBitSlice (toBitArray [ 1 2 3 ]) 1 3) 515; + # testBIT12 = assertEq (intFromBitSlice (toBitArray [ 1 2 3 ]) (-99) 99) 66051; + # testBIT13 = assertEq (intFromBitSlice (toBitArray [ ]) 1 3) 0; + testBIT14 = assertEq (byteArrayToInt (toBitArray [ 1 2 3 ])) 66051; + testBIT15 = assertEq (byteArrayToInt (toBitArray [ ])) 0; + testBIT16 = assertEq (byteArrayToUtf8String (toBitArray [ 226 150 136 65 ])) "█A"; +} null From 67e07a05f7d34a373e577a18ad44cb2e22e35074 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Mon, 9 Sep 2024 01:23:34 -0300 Subject: [PATCH 2/2] add nix prelude test to ci --- .github/workflows/ci.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bc71fbcf1..10af8b0c4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -587,6 +587,10 @@ jobs: run: make working-directory: ./test/javascript_prelude + - name: Test Nix prelude + run: make + working-directory: ./test/nix_prelude + - name: Test export of hex tarball run: make test working-directory: ./test/hextarball