extend("test")

describe("check builtin", () => {

  it("returns true for primitive type matches", () => {
    expect_equal(check("int", 123), true)
    expect_equal(check("string", "Guru"), true)
    expect_equal(check("float", 3.14), true)
    expect_equal(check("bool", false), true)
  })

  it("returns false for primitive type mismatches", () => {
    expect_equal(check("int", "not an int"), false)
    expect_equal(check("string", 42), false)
    expect_equal(check("float", "3.14"), false)
    expect_equal(check("bool", 1), false)
  })

  it("checks arrays of primitives", () => {
    expect_equal(check(["int"], [1,2,3]), true)
    expect_equal(check(["string"], ["a", "b", "c"]), true)
    expect_equal(check(["float"], [1.1, 2.2, 3.3]), true)
    expect_equal(check(["int"], ["a", 2, 3]), false)
    expect_equal(check(["string"], [1,2,3]), false)
  })

  it("checks keyed arrays (maps)", () => {
    schema = [ a: "int", b: "string" ]
    valid   = [ a: 7, b: "hi" ]
    missing = [ a: 7 ]          # missing key b
    wrong   = [ a: "x", b: "hi" ] # wrong type for a

    expect_equal(check(schema, valid), true)
    expect_equal(check(schema, missing), false)
    expect_equal(check(schema, wrong), false)
  })

  it("supports nested keyed arrays (maps)", () => {
    address_schema = [ city: "string", zip: "int" ]
    person_schema  = [ name: "string", address: address_schema ]

    good    = [ name: "Guru", address: [ city: "Paris", zip: 12345 ] ]
    no_zip  = [ name: "Guru", address: [ city: "Paris" ] ]
    bad_zip = [ name: "Guru", address: [ city: "Paris", zip: "notint" ] ]

    expect_equal(check(person_schema, good), true)
    expect_equal(check(person_schema, no_zip), false)
    expect_equal(check(person_schema, bad_zip), false)
  })

  it("allows extra keys in value (non-strict check)", () => {
    schema = [ x: "int" ]
    val    = [ x: 1, y: 2 ]
    expect_equal(check(schema, val), true)
  })

  it("checks arrays of keyed arrays", () => {
    row_schema = [ id: "int", label: "string" ]
    arr_schema = [ row_schema ]
    arr_good = [
      [ id: 1, label: "A" ],
      [ id: 2, label: "B" ]
    ]
    arr_bad = [
      [ id: 1, label: "A" ],
      [ id: "notint", label: "B" ]
    ]
    # The following will return true/false if array-of-keyed-array check is supported
    expect_equal(check(arr_schema, arr_good), true)
    expect_equal(check(arr_schema, arr_bad), false)
  })

  it("fails for mismatched root types", () => {
    expect_equal(check([ x: "int" ], 123), false)
    expect_equal(check("int", [ x: 7 ]), false)
  })

})
