Skip to content
File

Blob: src/rust/jsg-test/tests/eval.rs

rust180 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5use jsg::Number;
6 
7use crate::EvalError;
8 
9#[test]
10fn eval_returns_correct_type() {
11 let harness = crate::Harness::new();
12 harness.run_in_context(|lock, ctx| {
13 let result: String = ctx.eval(lock, "'Hello, World!'").unwrap();
14 assert_eq!(result, "Hello, World!");
15 Ok(())
16 });
17}
18 
19#[test]
20fn eval_string_concatenation() {
21 let harness = crate::Harness::new();
22 harness.run_in_context(|lock, ctx| {
23 let result: String = ctx.eval(lock, "'Hello' + ', ' + 'World!'").unwrap();
24 assert_eq!(result, "Hello, World!");
25 Ok(())
26 });
27}
28 
29#[test]
30fn eval_number_returns_number_type() {
31 let harness = crate::Harness::new();
32 harness.run_in_context(|lock, ctx| {
33 let result: Number = ctx.eval(lock, "42").unwrap();
34 assert!((result.value() - 42.0).abs() < f64::EPSILON);
35 Ok(())
36 });
37}
38 
39#[test]
40fn eval_arithmetic_expression() {
41 let harness = crate::Harness::new();
42 harness.run_in_context(|lock, ctx| {
43 let result: Number = ctx.eval(lock, "1 + 2 + 3").unwrap();
44 assert!((result.value() - 6.0).abs() < f64::EPSILON);
45 Ok(())
46 });
47}
48 
49#[test]
50fn eval_boolean_true() {
51 let harness = crate::Harness::new();
52 harness.run_in_context(|lock, ctx| {
53 let result: bool = ctx.eval(lock, "true").unwrap();
54 assert!(result);
55 Ok(())
56 });
57}
58 
59#[test]
60fn eval_boolean_false() {
61 let harness = crate::Harness::new();
62 harness.run_in_context(|lock, ctx| {
63 let result: bool = ctx.eval(lock, "false").unwrap();
64 assert!(!result);
65 Ok(())
66 });
67}
68 
69#[test]
70fn eval_comparison_expression() {
71 let harness = crate::Harness::new();
72 harness.run_in_context(|lock, ctx| {
73 let result: bool = ctx.eval(lock, "5 > 3").unwrap();
74 assert!(result);
75 Ok(())
76 });
77}
78 
79#[test]
80fn eval_null() {
81 let harness = crate::Harness::new();
82 harness.run_in_context(|lock, ctx| {
83 let result: jsg::Nullable<bool> = ctx.eval(lock, "null").unwrap();
84 assert!(result.is_null());
85 Ok(())
86 });
87}
88 
89#[test]
90fn eval_throws_on_error() {
91 let harness = crate::Harness::new();
92 harness.run_in_context(|lock, ctx| {
93 let result = ctx.eval::<bool>(lock, "throw new Error('test error')");
94 match result.unwrap_err() {
95 EvalError::Exception(value) => {
96 assert_eq!(value.to_string(), "Error: test error");
97 }
98 _ => panic!("Unexpected error type"),
99 }
100 Ok(())
101 });
102}
103 
104#[test]
105fn eval_throws_string_preserves_message() {
106 let harness = crate::Harness::new();
107 harness.run_in_context(|lock, ctx| {
108 let result = ctx.eval::<bool>(lock, "throw 'custom string error'");
109 match result.unwrap_err() {
110 EvalError::Exception(value) => {
111 assert_eq!(value.to_string(), "custom string error");
112 }
113 _ => panic!("Unexpected error type"),
114 }
115 Ok(())
116 });
117}
118 
119#[test]
120fn eval_function_call() {
121 let harness = crate::Harness::new();
122 harness.run_in_context(|lock, ctx| {
123 let result: String = ctx
124 .eval(lock, "(function() { return 'from function'; })()")
125 .unwrap();
126 assert_eq!(result, "from function");
127 Ok(())
128 });
129}
130 
131#[test]
132fn eval_typeof_string() {
133 let harness = crate::Harness::new();
134 harness.run_in_context(|lock, ctx| {
135 let result: String = ctx.eval(lock, "typeof 'hello'").unwrap();
136 assert_eq!(result, "string");
137 Ok(())
138 });
139}
140 
141#[test]
142fn eval_typeof_number() {
143 let harness = crate::Harness::new();
144 harness.run_in_context(|lock, ctx| {
145 let result: String = ctx.eval(lock, "typeof 42").unwrap();
146 assert_eq!(result, "number");
147 Ok(())
148 });
149}
150 
151#[test]
152fn eval_typeof_boolean() {
153 let harness = crate::Harness::new();
154 harness.run_in_context(|lock, ctx| {
155 let result: String = ctx.eval(lock, "typeof true").unwrap();
156 assert_eq!(result, "boolean");
157 Ok(())
158 });
159}
160 
161#[test]
162fn eval_unicode_string() {
163 let harness = crate::Harness::new();
164 harness.run_in_context(|lock, ctx| {
165 let result: String = ctx.eval(lock, "'こんにちは'").unwrap();
166 assert_eq!(result, "こんにちは");
167 Ok(())
168 });
169}
170 
171#[test]
172fn eval_emoji_string() {
173 let harness = crate::Harness::new();
174 harness.run_in_context(|lock, ctx| {
175 let result: String = ctx.eval(lock, "'😀🎉'").unwrap();
176 assert_eq!(result, "😀🎉");
177 Ok(())
178 });
179}