Skip to content

Commit

Permalink
enum and struct is_valid_data unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christianschmitz committed Sep 17, 2024
1 parent 7cad006 commit d7cfcb1
Show file tree
Hide file tree
Showing 2 changed files with 252 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/enum.test.js
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)
})
})
})
179 changes: 179 additions & 0 deletions test/struct.test.js
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)
})
})
})

0 comments on commit d7cfcb1

Please sign in to comment.