test_expression_parser.rb 1.85 KB
Newer Older
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
[ '../lib', 'lib' ].each { |d| $:.unshift(d) if File::directory?(d) }
require 'minitest/autorun'
require 'cconfigspace'

class CConfigSpaceTestExpressionParser < Minitest::Test
  def setup
    CCS.init
  end

  def test_parse
    m = CCS::ExpressionParser.new.method(:parse)
    exp = "1.0 + 1 == 2 || +1 == 3e0 && \"y\\nes\" == 'no' "
    res = m[exp]
    assert( res.kind_of? CCS::Expression )
    assert_equal( "1.0 + 1 == 2 || +1 == 3.0 && \"y\\nes\" == \"no\"", res.to_s )
  end

  def test_parse_priority
    m = CCS::ExpressionParser.new.method(:parse)
    exp = "(1 + 3) * 2"
    res = m[exp]
    assert( res.kind_of? CCS::Expression )
    assert_equal( exp, res.to_s )
  end

  def test_associativity
    m = CCS::ExpressionParser.new.method(:parse)
    exp = "5 - 2 - 1"
    res = m[exp]
    assert( res.kind_of? CCS::Expression )
    assert_equal( 2, res.eval )
    exp = "5 - +(+2 - 1)"
    res = m[exp]
    assert( res.kind_of? CCS::Expression )
    assert_equal( 4, res.eval )
  end
37
38
39
40
41
42
43
44
45
46
47
48

  def test_in
    m = CCS::ExpressionParser.new.method(:parse)
    exp = "5 # [3.0, 5]"
    res = m[exp]
    assert( res.kind_of? CCS::Expression )
    assert_equal( true, res.eval )
    exp = "5 # [3.0, 4]"
    res = m[exp]
    assert( res.kind_of? CCS::Expression )
    assert_equal( false, res.eval )
  end
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

  def test_boolean
    m = CCS::ExpressionParser.new.method(:parse)
    exp = "true"
    res = m[exp]
    assert( res.kind_of? CCS::Literal )
    assert_equal( true, res.eval )
    assert_equal( "true", res.to_s )
    exp = "false"
    res = m[exp]
    assert( res.kind_of? CCS::Literal )
    assert_equal( false, res.eval )
    assert_equal( "false", res.to_s )
  end

  def test_none
    m = CCS::ExpressionParser.new.method(:parse)
Brice Videau's avatar
Brice Videau committed
66
    exp = "none"
67
68
69
    res = m[exp]
    assert( res.kind_of? CCS::Literal )
    assert_nil( res.eval )
Brice Videau's avatar
Brice Videau committed
70
    assert_equal( "none", res.to_s )
71
72
  end

73
74
end