summaryrefslogtreecommitdiff
path: root/cli/tests/eval_tests.rs
blob: 7107ecbbb35e55576ed52ffea8fa8bfea6ce2c7e (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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

mod integration;

use test_util as util;

mod eval {
  use super::*;

  #[test]
  fn eval_p() {
    let output = util::deno_cmd()
      .arg("eval")
      .arg("-p")
      .arg("1+2")
      .stdout(std::process::Stdio::piped())
      .spawn()
      .unwrap()
      .wait_with_output()
      .unwrap();
    assert!(output.status.success());
    let stdout_str = util::strip_ansi_codes(
      std::str::from_utf8(&output.stdout).unwrap().trim(),
    );
    assert_eq!("3", stdout_str);
  }

  // Make sure that snapshot flags don't affect runtime.
  #[test]
  fn eval_randomness() {
    let mut numbers = Vec::with_capacity(10);
    for _ in 0..10 {
      let output = util::deno_cmd()
        .arg("eval")
        .arg("-p")
        .arg("Math.random()")
        .stdout(std::process::Stdio::piped())
        .spawn()
        .unwrap()
        .wait_with_output()
        .unwrap();
      assert!(output.status.success());
      let stdout_str = util::strip_ansi_codes(
        std::str::from_utf8(&output.stdout).unwrap().trim(),
      );
      numbers.push(stdout_str.to_string());
    }
    numbers.dedup();
    assert!(numbers.len() > 1);
  }

  itest!(eval_basic {
    args: "eval console.log(\"hello\")",
    output_str: Some("hello\n"),
  });

  // Ugly parentheses due to whitespace delimiting problem.
  itest!(eval_ts {
    args: "eval --quiet --ext=ts console.log((123)as(number))", // 'as' is a TS keyword only
    output_str: Some("123\n"),
  });

  itest!(dyn_import_eval {
    args: "eval import('./subdir/mod4.js').then(console.log)",
    output: "eval/dyn_import_eval.out",
  });

  // Cannot write the expression to evaluate as "console.log(typeof gc)"
  // because itest! splits args on whitespace.
  itest!(v8_flags_eval {
    args: "eval --v8-flags=--expose-gc console.log(typeof(gc))",
    output: "run/v8_flags.js.out",
  });

  itest!(check_local_by_default {
    args: "eval --quiet import('http://localhost:4545/subdir/type_error.ts').then(console.log);",
    output: "eval/check_local_by_default.out",
    http_server: true,
  });

  itest!(check_local_by_default2 {
    args: "eval --quiet import('./eval/check_local_by_default2.ts').then(console.log);",
    output: "eval/check_local_by_default2.out",
    http_server: true,
  });
}