-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enum and struct is_valid_data unit-tests
- Loading branch information
1 parent
7cad006
commit d7cfcb1
Showing
2 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { describe, it } from "node:test" | ||
import { | ||
False, | ||
True, | ||
bytes, | ||
compileForRun, | ||
constr, | ||
int, | ||
list, | ||
map | ||
} from "./utils.js" | ||
|
||
describe("Enum with two variants", () => { | ||
describe("is_valid_data", () => { | ||
const runner = | ||
compileForRun(`testing enum_with_two_variants_is_valid_data | ||
enum E { | ||
A | ||
B { | ||
a: Int | ||
b: Int | ||
} | ||
} | ||
func main(d: Data) -> Bool { | ||
E::is_valid_data(d) | ||
}`) | ||
|
||
it("returns true for constrData with tag 0 and no fields", () => { | ||
runner([constr(0)], True) | ||
}) | ||
|
||
it("returns false for constrData with tag -1 and no fields", () => { | ||
runner([constr(-1)], False) | ||
}) | ||
|
||
it("returns false for constrData with tag 0 and one field", () => { | ||
runner([constr(0, int(0))], False) | ||
}) | ||
|
||
it("returns true for constrData with tag 1 and two fields", () => { | ||
runner([constr(1, int(0), int(1))], True) | ||
}) | ||
|
||
it("returns false for constrData with tag 1 and one of the two fields isn't iData", () => { | ||
runner([constr(1, int(0), bytes([]))], False) | ||
}) | ||
|
||
it("returns false for constrData with tag 1 and too many fields", () => { | ||
runner([constr(1, int(0), int(1), int(1))], False) | ||
}) | ||
|
||
it("returns false for constrData with tag 2 and two fields", () => { | ||
runner([constr(2, int(0), int(1))], False) | ||
}) | ||
|
||
it("returns false for iData", () => { | ||
runner([int(0)], False) | ||
}) | ||
|
||
it("returns false for bData", () => { | ||
runner([bytes([])], False) | ||
}) | ||
|
||
it("returns false for listData", () => { | ||
runner([list()], False) | ||
}) | ||
|
||
it("returns false for mapData", () => { | ||
runner([map([])], False) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { describe, it } from "node:test" | ||
import { | ||
False, | ||
True, | ||
bytes, | ||
compileForRun, | ||
constr, | ||
int, | ||
list, | ||
map | ||
} from "./utils.js" | ||
import { encodeUtf8 } from "@helios-lang/codec-utils" | ||
|
||
describe("Singleton", () => { | ||
describe("Singleton(Int)::is_valid_data", () => { | ||
const runner = compileForRun(`testing singleton_int_is_valid_data | ||
struct S { | ||
a: Int | ||
} | ||
func main(d: Data) -> Bool { | ||
S::is_valid_data(d) | ||
}`) | ||
|
||
it("returns true for iData", () => { | ||
runner([int(0)], True) | ||
}) | ||
|
||
it("returns false for bData", () => { | ||
runner([bytes([])], False) | ||
}) | ||
|
||
it("returns false for listData", () => { | ||
runner([list()], False) | ||
}) | ||
|
||
it("returns false for mapData", () => { | ||
runner([map([])], False) | ||
}) | ||
|
||
it("returns false for constrData", () => { | ||
runner([constr(1)], False) | ||
}) | ||
}) | ||
}) | ||
|
||
describe("Pair[Int, Int]", () => { | ||
describe("Pair[Int, Int]::is_valid_data", () => { | ||
const runner = compileForRun(`testing pair_int_int_is_valid_data | ||
struct S { | ||
a: Int | ||
b: Int | ||
} | ||
func main(d: Data) -> Bool { | ||
S::is_valid_data(d) | ||
}`) | ||
|
||
it("returns true for list with two iData items", () => { | ||
runner([list(int(0), int(1))], True) | ||
}) | ||
|
||
it("returns false for list with three iData items", () => { | ||
runner([list(int(0), int(1), int(2))], False) | ||
}) | ||
|
||
it("returns false for list with one iData item", () => { | ||
runner([list(int(0))], False) | ||
}) | ||
|
||
it("returns false for iData", () => { | ||
runner([int(0)], False) | ||
}) | ||
|
||
it("returns false for bData", () => { | ||
runner([bytes([])], False) | ||
}) | ||
|
||
it("returns false for listData", () => { | ||
runner([list()], False) | ||
}) | ||
|
||
it("returns false for mapData", () => { | ||
runner([map([])], False) | ||
}) | ||
|
||
it("returns false for constrData", () => { | ||
runner([constr(1)], False) | ||
}) | ||
}) | ||
}) | ||
|
||
describe("Cip68 Pair[Int, Int]", () => { | ||
describe("Pair[Int, Int]::is_valid_data", () => { | ||
const runner = compileForRun(`testing pair_int_int_is_valid_data | ||
struct S { | ||
a: Int "a" | ||
b: Int "b" | ||
} | ||
func main(d: Data) -> Bool { | ||
S::is_valid_data(d) | ||
}`) | ||
|
||
it("returns true for constrData with tag 0 and one field containing the map", () => { | ||
runner( | ||
[ | ||
constr( | ||
0, | ||
map([ | ||
[bytes(encodeUtf8("a")), int(0)], | ||
[bytes(encodeUtf8("b")), int(1)] | ||
]) | ||
) | ||
], | ||
True | ||
) | ||
}) | ||
|
||
it("returns false if one of the fields isn't iData", () => { | ||
runner( | ||
[ | ||
constr( | ||
0, | ||
map([ | ||
[bytes(encodeUtf8("a")), int(0)], | ||
[bytes(encodeUtf8("b")), bytes([])] | ||
]) | ||
) | ||
], | ||
False | ||
) | ||
}) | ||
|
||
it("returns false if one of the fields is missing", () => { | ||
runner([constr(0, map([[bytes(encodeUtf8("a")), int(0)]]))], False) | ||
}) | ||
|
||
it("returns true even if an unknown field is included", () => { | ||
runner( | ||
[ | ||
constr( | ||
0, | ||
map([ | ||
[bytes(encodeUtf8("b")), int(1)], | ||
[bytes(encodeUtf8("a")), int(0)], | ||
[bytes(encodeUtf8("c")), int(2)] | ||
]) | ||
) | ||
], | ||
True | ||
) | ||
}) | ||
|
||
it("returns false for list", () => { | ||
runner([list()], False) | ||
}) | ||
|
||
it("returns false for iData", () => { | ||
runner([int(0)], False) | ||
}) | ||
|
||
it("returns false for bData", () => { | ||
runner([bytes([])], False) | ||
}) | ||
|
||
it("returns false for listData", () => { | ||
runner([list()], False) | ||
}) | ||
|
||
it("returns false for mapData", () => { | ||
runner([map([])], False) | ||
}) | ||
|
||
it("returns false for constrData with tag 1", () => { | ||
runner([constr(1)], False) | ||
}) | ||
}) | ||
}) |