File: test_css_parsing_tests.rb

package info (click to toggle)
ruby-crass 1.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 824 kB
  • sloc: ruby: 2,158; python: 202; makefile: 7
file content (150 lines) | stat: -rw-r--r-- 3,705 bytes parent folder | download | duplicates (4)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# encoding: utf-8

# This file loads and runs Simon Sapin's CSS parsing tests, which live under the
# test/css-parsing-tests directory. The original test repo can be found at:
#
# https://github.com/SimonSapin/css-parsing-tests/

require 'json'
require_relative 'support/common'

def load_css_tests(filename)
  JSON.parse(File.read(File.join(File.dirname(__FILE__), "/css-parsing-tests/#{filename}")))
end

describe 'CSS Parsing Tests' do
  describe 'component_value_list' do
    make_my_diffs_pretty!
    parallelize_me!

    tests = load_css_tests('component_value_list.json')

    tests.each_slice(2) do |test|
      css      = test[0]
      expected = test[1]

      it "should parse: #{css.gsub("\n", "\\n")}" do
        parser = Crass::Parser.new(css)
        assert_equal(expected, translate_tokens(parser.parse_component_values))
      end
    end
  end

  describe 'declaration_list' do
    make_my_diffs_pretty!
    parallelize_me!

    tests = load_css_tests('declaration_list.json')

    tests.each_slice(2) do |test|
      css      = test[0]
      expected = test[1]

      it "should parse: #{css.gsub("\n", "\\n")}" do
        parser = Crass::Parser.new(css)
        assert_equal(expected, translate_tokens(parser.parse_declarations(parser.tokens, {:strict => true})))
      end
    end
  end

  describe 'one_component_value' do
    make_my_diffs_pretty!
    parallelize_me!

    tests = load_css_tests('one_component_value.json')

    tests.each_slice(2) do |test|
      css      = test[0]
      expected = test[1]

      it "should parse: #{css.gsub("\n", "\\n")}" do
        parser = Crass::Parser.new(css)
        assert_equal(expected, translate_tokens(parser.parse_component_value)[0])
      end
    end
  end

  describe 'one_declaration' do
    make_my_diffs_pretty!
    parallelize_me!

    tests = load_css_tests('one_declaration.json')

    tests.each_slice(2) do |test|
      css      = test[0]
      expected = test[1]

      it "should parse: #{css.gsub("\n", "\\n")}" do
        parser = Crass::Parser.new(css)
        assert_equal(expected, translate_tokens(parser.parse_declaration)[0])
      end
    end
  end

  describe 'one_rule' do
    make_my_diffs_pretty!
    parallelize_me!

    tests = load_css_tests('one_rule.json')

    tests.each_slice(2) do |test|
      css      = test[0]
      expected = test[1]

      it "should parse: #{css.gsub("\n", "\\n")}" do
        parser = Crass::Parser.new(css)
        assert_equal(expected, translate_tokens(parser.parse_rule)[0])
      end
    end
  end

  describe 'rule_list' do
    make_my_diffs_pretty!
    parallelize_me!

    tests = load_css_tests('rule_list.json')

    tests.each_slice(2) do |test|
      css      = test[0]
      expected = test[1]

      it "should parse: #{css.gsub("\n", "\\n")}" do
        parser = Crass::Parser.new(css)
        rules  = parser.consume_rules

        # Remove non-standard whitespace tokens.
        rules.reject! do |token|
          node = token[:node]
          node == :whitespace
        end

        assert_equal(expected, translate_tokens(rules))
      end
    end
  end

  describe 'stylesheet' do
    make_my_diffs_pretty!
    parallelize_me!

    tests = load_css_tests('stylesheet.json')

    tests.each_slice(2) do |test|
      css      = test[0]
      expected = test[1]

      it "should parse: #{css.gsub("\n", "\\n")}" do
        parser = Crass::Parser.new(css)
        rules  = parser.consume_rules(:top_level => true)

        # Remove non-standard whitespace tokens.
        rules.reject! do |token|
          node = token[:node]
          node == :whitespace
        end

        assert_equal(expected, translate_tokens(rules))
      end
    end
  end
end