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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
# encoding: utf-8
gem 'minitest'
require 'minitest/autorun'
require 'crass'
CP = Crass::Parser
CT = Crass::Tokenizer
# Hack shared test support into Minitest.
Minitest::Spec.class_eval do
def self.shared_tests
@shared_tests ||= {}
end
end
module Minitest::Spec::SharedTests
def behaves_like(desc)
self.instance_eval(&Minitest::Spec.shared_tests[desc])
end
def shared_tests_for(desc, &block)
Minitest::Spec.shared_tests[desc] = block
end
end
Object.class_eval { include Minitest::Spec::SharedTests }
# Custom assertions and helpers.
def assert_tokens(input, actual, offset = 0, options = {})
actual = [actual] unless actual.is_a?(Array)
tokens = tokenize(input, offset, options)
assert_equal tokens, actual
end
def reposition_tokens(tokens, offset)
tokens.each {|token| token[:pos] += offset }
tokens
end
def tokenize(input, offset = 0, options = {})
tokens = CT.tokenize(input, options)
reposition_tokens(tokens, offset) unless offset == 0
tokens
end
# Translates Crass tokens into a form that can be compared to the expected
# values of Simon Sapin's CSS parsing tests.
#
# https://github.com/SimonSapin/css-parsing-tests/#result-representation
def translate_tokens(tokens)
return [] if tokens.nil?
translated = []
tokens = [tokens] unless tokens.is_a?(Array)
tokens.each do |token|
value = token[:value]
result = case token[:node]
# Rules and declarations.
when :at_rule
['at-rule', token[:name], translate_tokens(token[:prelude]), token[:block] ? translate_tokens(token[:block]) : nil]
when :qualified_rule
['qualified rule', translate_tokens(token[:prelude]), token[:block] ? translate_tokens(token[:block]) : nil]
when :declaration
['declaration', token[:name], translate_tokens(value), token[:important]]
# Component values.
when :at_keyword
['at-keyword', value]
when :bad_string
['error', 'bad-string']
when :bad_url
['error', 'bad-url']
when :cdc
'-->'
when :cdo
'<!--'
when :colon
':'
when :column
'||'
when :comma
','
when :dash_match
'|='
when :delim
value
when :dimension
['dimension', token[:repr], value, token[:type].to_s, token[:unit]]
when :error
['error', value]
when :function
if token[:name]
['function', token[:name]].concat(translate_tokens(value))
else
['function', value]
end
when :hash
['hash', value, token[:type].to_s]
when :ident
['ident', value]
when :include_match
'~='
when :number
['number', token[:repr], value, token[:type].to_s]
when :percentage
['percentage', token[:repr], value, token[:type].to_s]
when :prefix_match
'^='
when :semicolon
';'
when :simple_block
[token[:start] + token[:end]].concat(translate_tokens(value))
when :string
['string', value]
when :substring_match
'*='
when :suffix_match
'$='
when :unicode_range
['unicode-range', token[:start], token[:end]]
when :url
['url', value]
when :whitespace
' '
when :'}', :']', :')'
['error', token[:node].to_s]
else
nil
end
translated << result unless result.nil?
end
translated
end
|