summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_type_checking.js
blob: bfeb07c82befc88c5293bd5e32041fe551638f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const Arithmetic = ChromeUtils.importESModule(
  "resource://gre/modules/RustArithmetic.sys.mjs"
);
const Geometry = ChromeUtils.importESModule(
  "resource://gre/modules/RustGeometry.sys.mjs"
);
const TodoList = ChromeUtils.importESModule(
  "resource://gre/modules/RustTodolist.sys.mjs"
);
const Rondpoint = ChromeUtils.importESModule(
  "resource://gre/modules/RustRondpoint.sys.mjs"
);
const { UniFFITypeError } = ChromeUtils.importESModule(
  "resource://gre/modules/UniFFI.sys.mjs"
);

add_task(async function testFunctionArguments() {
  await Assert.rejects(
    Arithmetic.add(2),
    UniFFITypeError,
    "add() call missing argument"
  );
  Assert.throws(
    () => new Geometry.Point(0.0),
    UniFFITypeError,
    "Point constructor missing argument"
  );
});

add_task(async function testObjectPointers() {
  const todo = await TodoList.TodoList.init();
  const stringifier = await Rondpoint.Stringifier.init();
  await todo.getEntries(); // OK
  todo[TodoList.UnitTestObjs.uniffiObjectPtr] =
    stringifier[Rondpoint.UnitTestObjs.uniffiObjectPtr];

  await Assert.rejects(
    todo.getEntries(), // the pointer is incorrect, should throw
    /Bad pointer type/,
    "getEntries() with wrong pointer type"
  );
});

add_task(async function testEnumTypeCheck() {
  await Assert.rejects(
    Rondpoint.copieEnumeration("invalid"), // Not an integer value
    /e:/, // Ensure exception message includes the argument name
    "copieEnumeration() with non-Enumeration value should throw"
  );
  await Assert.rejects(
    Rondpoint.copieEnumeration(0), // Integer, but doesn't map to a variant
    /e:/, // Ensure exception message includes the argument name
    "copieEnumeration() with non-Enumeration value should throw"
  );
  await Assert.rejects(
    Rondpoint.copieEnumeration(4), // Integer, but doesn't map to a variant
    /e:/, // Ensure exception message includes the argument name
    "copieEnumeration() with non-Enumeration value should throw"
  );
});

add_task(async function testRecordTypeCheck() {
  await Assert.rejects(
    Geometry.gradient(123), // Not a Line object
    UniFFITypeError,
    "gradient with non-Line object should throw"
  );

  await Assert.rejects(
    Geometry.gradient({
      start: {
        coordX: 0.0,
        coordY: 0.0,
      },
      // missing the end field
    }),
    /ln.end/, // Ensure exception message includes the argument name
    "gradient with Line object with missing end field should throw"
  );
});

add_task(async function testOptionTypeCheck() {
  const optionneur = await Rondpoint.Optionneur.init();
  await Assert.rejects(
    optionneur.sinonNull(0),
    UniFFITypeError,
    "sinonNull with non-string should throw"
  );
});

add_task(async function testSequenceTypeCheck() {
  const todo = await TodoList.TodoList.init();
  await Assert.rejects(
    todo.addEntries("not a list"),
    UniFFITypeError,
    "addEntries with non-list should throw"
  );

  await Assert.rejects(
    todo.addEntries(["not TodoEntry"]),
    /entries\[0]/,
    "addEntries with non TodoEntry item should throw"
  );
});

add_task(async function testMapTypeCheck() {
  await Assert.rejects(
    Rondpoint.copieCarte("not a map"),
    UniFFITypeError,
    "copieCarte with a non-map should throw"
  );

  await Assert.rejects(
    Rondpoint.copieCarte({ x: 1 }),
    /c\[x]/,
    "copieCarte with a wrong value type should throw"
  );

  // TODO: test key types once we implement https://bugzilla.mozilla.org/show_bug.cgi?id=1809459
});