summaryrefslogtreecommitdiff
path: root/test/functional/word_list.rb
blob: 84d6e9e793371bba1774c70425a90157a836d576 (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
require 'test/unit'
require 'coderay'

class WordListTest < Test::Unit::TestCase
  
  include CodeRay
  
  # define word arrays
  RESERVED_WORDS = %w[
    asm break case continue default do else
    ...
  ]
  
  PREDEFINED_TYPES = %w[
    int long short char void
    ...
  ]
  
  PREDEFINED_CONSTANTS = %w[
    EOF NULL ...
  ]
  
  # make a WordList
  IDENT_KIND = WordList.new(:ident).
    add(RESERVED_WORDS, :reserved).
    add(PREDEFINED_TYPES, :pre_type).
    add(PREDEFINED_CONSTANTS, :pre_constant)

  def test_word_list_example
    assert_equal :pre_type, IDENT_KIND['void']
    # assert_equal :pre_constant, IDENT_KIND['...']  # not specified
  end
  
  def test_word_list
    list = WordList.new(:ident).add(['foobar'], :reserved)
    assert_equal :reserved, list['foobar']
    assert_equal :ident, list['FooBar']
  end

  def test_word_list_cached
    list = WordList.new(:ident, true).add(['foobar'], :reserved)
    assert_equal :reserved, list['foobar']
    assert_equal :ident, list['FooBar']
  end

  def test_case_ignoring_word_list
    list = CaseIgnoringWordList.new(:ident).add(['foobar'], :reserved)
    assert_equal :ident, list['foo']
    assert_equal :reserved, list['foobar']
    assert_equal :reserved, list['FooBar']

    list = CaseIgnoringWordList.new(:ident).add(['FooBar'], :reserved)
    assert_equal :ident, list['foo']
    assert_equal :reserved, list['foobar']
    assert_equal :reserved, list['FooBar']
  end

  def test_case_ignoring_word_list_cached
    list = CaseIgnoringWordList.new(:ident, true).add(['foobar'], :reserved)
    assert_equal :ident, list['foo']
    assert_equal :reserved, list['foobar']
    assert_equal :reserved, list['FooBar']

    list = CaseIgnoringWordList.new(:ident, true).add(['FooBar'], :reserved)
    assert_equal :ident, list['foo']
    assert_equal :reserved, list['foobar']
    assert_equal :reserved, list['FooBar']
  end

  def test_dup
    list = WordList.new(:ident).add(['foobar'], :reserved)
    assert_equal :reserved, list['foobar']
    list2 = list.dup
    list2.add(%w[foobar], :keyword)
    assert_equal :keyword, list2['foobar']
    assert_equal :reserved, list['foobar']
  end

end