# -*- coding: utf-8 -*- require File.dirname(__FILE__) + '/../test_helper' require File.dirname(__FILE__) + '/test_helper' require 'sass/engine' require 'stringio' require 'mock_importer' require 'pathname' module Sass::Script::Functions::UserFunctions def option(name) Sass::Script::Value::String.new(@options[name.value.to_sym].to_s) end def set_a_variable(name, value) environment.set_var(name.value, value) return Sass::Script::Value::Null.new end def set_a_global_variable(name, value) environment.set_global_var(name.value, value) return Sass::Script::Value::Null.new end def get_a_variable(name) environment.var(name.value) || Sass::Script::Value::String.new("undefined") end end module Sass::Script::Functions include Sass::Script::Functions::UserFunctions end class SassEngineTest < MiniTest::Test FAKE_FILE_NAME = __FILE__.gsub(/rb$/,"sass") # A map of erroneous Sass documents to the error messages they should produce. # The error messages may be arrays; # if so, the second element should be the line number that should be reported for the error. # If this isn't provided, the tests will assume the line number should be the last line of the document. EXCEPTION_MAP = { "$a: 1 + " => 'Invalid CSS after "1 +": expected expression (e.g. 1px, bold), was ""', "$a: 1 + 2 +" => 'Invalid CSS after "1 + 2 +": expected expression (e.g. 1px, bold), was ""', "$a: 1 + 2 + %" => 'Invalid CSS after "1 + 2 + ": expected expression (e.g. 1px, bold), was "%"', "$a: foo(\"bar\"" => 'Invalid CSS after "foo("bar"": expected ")", was ""', "$a: 1 }" => 'Invalid CSS after "1 ": expected expression (e.g. 1px, bold), was "}"', "$a: 1 }foo\"" => 'Invalid CSS after "1 ": expected expression (e.g. 1px, bold), was "}foo""', ":" => 'Invalid property: ":".', ": a" => 'Invalid property: ": a".', "a\n :b" => < 'Invalid property: "b:" (no value).', "a\n :b: c" => 'Invalid property: ":b: c".', "a\n :b:c d" => 'Invalid property: ":b:c d".', "a\n :b c;" => 'Invalid CSS after "c": expected expression (e.g. 1px, bold), was ";"', "a\n b: c;" => 'Invalid CSS after "c": expected expression (e.g. 1px, bold), was ";"', ".foo ^bar\n a: b" => ['Invalid CSS after ".foo ": expected selector, was "^bar"', 1], "a\n @extend .foo ^bar" => 'Invalid CSS after ".foo ": expected selector, was "^bar"', "a\n @extend .foo .bar" => "Can't extend .foo .bar: can't extend nested selectors", "a\n @extend >" => "Can't extend >: invalid selector", "a\n @extend &.foo" => "Can't extend &.foo: can't extend parent selectors", "a: b" => 'Properties are only allowed within rules, directives, mixin includes, or other properties.', ":a b" => 'Properties are only allowed within rules, directives, mixin includes, or other properties.', "$" => 'Invalid variable: "$".', "$a" => 'Invalid variable: "$a".', "$ a" => 'Invalid variable: "$ a".', "$a b" => 'Invalid variable: "$a b".', "$a: 1b + 2c" => "Incompatible units: 'c' and 'b'.", "$a: 1b < 2c" => "Incompatible units: 'c' and 'b'.", "$a: 1b > 2c" => "Incompatible units: 'c' and 'b'.", "$a: 1b <= 2c" => "Incompatible units: 'c' and 'b'.", "$a: 1b >= 2c" => "Incompatible units: 'c' and 'b'.", "a\n b: 1b * 2c" => "2b*c isn't a valid CSS value.", "a\n b: 1b % 2c" => "Incompatible units: 'c' and 'b'.", "$a: 2px + #ccc" => "Cannot add a number with units (2px) to a color (#ccc).", "$a: #ccc + 2px" => "Cannot add a number with units (2px) to a color (#ccc).", "& a\n :b c" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1], "a\n :b\n c" => "Illegal nesting: Only properties may be nested beneath properties.", "$a: b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath variable declarations.", "@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.", "foo\n @import foo.css" => "CSS import directives may only be used at the root of a document.", "@if true\n @import foo" => "Import directives may not be used within control directives or mixins.", "@if true\n .foo\n @import foo" => "Import directives may not be used within control directives or mixins.", "@mixin foo\n @import foo" => "Import directives may not be used within control directives or mixins.", "@mixin foo\n .foo\n @import foo" => "Import directives may not be used within control directives or mixins.", "@import foo;" => "Invalid @import: expected end of line, was \";\".", '$foo: "bar" "baz" !' => %Q{Invalid CSS after ""bar" "baz" ": expected expression (e.g. 1px, bold), was "!"}, '$foo: "bar" "baz" $' => %Q{Invalid CSS after ""bar" "baz" ": expected expression (e.g. 1px, bold), was "$"}, #' "=foo\n :color red\n.bar\n +bang" => "Undefined mixin 'bang'.", "=foo\n :color red\n.bar\n +bang_bop" => "Undefined mixin 'bang_bop'.", "=foo\n :color red\n.bar\n +bang-bop" => "Undefined mixin 'bang-bop'.", ".foo\n =foo\n :color red\n.bar\n +foo" => "Undefined mixin 'foo'.", " a\n b: c" => ["Indenting at the beginning of the document is illegal.", 1], " \n \n\t\n a\n b: c" => ["Indenting at the beginning of the document is illegal.", 4], "a\n b: c\n b: c" => ["Inconsistent indentation: 1 space was used for indentation, but the rest of the document was indented using 2 spaces.", 3], "a\n b: c\na\n b: c" => ["Inconsistent indentation: 1 space was used for indentation, but the rest of the document was indented using 2 spaces.", 4], "a\n\t\tb: c\n\tb: c" => ["Inconsistent indentation: 1 tab was used for indentation, but the rest of the document was indented using 2 tabs.", 3], "a\n b: c\n b: c" => ["Inconsistent indentation: 3 spaces were used for indentation, but the rest of the document was indented using 2 spaces.", 3], "a\n b: c\n a\n d: e" => ["Inconsistent indentation: 3 spaces were used for indentation, but the rest of the document was indented using 2 spaces.", 4], "a\n \tb: c" => ["Indentation can't use both tabs and spaces.", 2], "=a(" => 'Invalid CSS after "(": expected variable (e.g. $foo), was ""', "=a(b)" => 'Invalid CSS after "(": expected variable (e.g. $foo), was "b)"', "=a(,)" => 'Invalid CSS after "(": expected variable (e.g. $foo), was ",)"', "=a($)" => 'Invalid CSS after "(": expected variable (e.g. $foo), was "$)"', "=a($foo bar)" => 'Invalid CSS after "($foo ": expected ")", was "bar)"', "=foo\n bar: baz\n+foo" => ["Properties are only allowed within rules, directives, mixin includes, or other properties.", 2], "a-\#{$b\n c: d" => ['Invalid CSS after "a-#{$b": expected "}", was ""', 1], "=a($b: 1, $c)" => "Required argument $c must come before any optional arguments.", "=a($b: 1)\n a: $b\ndiv\n +a(1,2)" => "Mixin a takes 1 argument but 2 were passed.", "=a($b: 1)\n a: $b\ndiv\n +a(1,$c: 3)" => "Mixin a doesn't have an argument named $c.", "=a($b)\n a: $b\ndiv\n +a" => "Mixin a is missing argument $b.", "@function foo()\n 1 + 2" => "Functions can only contain variable declarations and control directives.", "@function foo()\n foo: bar" => "Functions can only contain variable declarations and control directives.", "@function foo()\n foo: bar\n @return 3" => ["Functions can only contain variable declarations and control directives.", 2], "@function foo\n @return 1" => ['Invalid CSS after "": expected "(", was ""', 1], "@function foo(\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was ""', 1], "@function foo(b)\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was "b)"', 1], "@function foo(,)\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was ",)"', 1], "@function foo($)\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was "$)"', 1], "@function foo()\n @return" => 'Invalid @return: expected expression.', "@function foo()\n @return 1\n $var: val" => 'Illegal nesting: Nothing may be nested beneath return directives.', "@function foo($a)\n @return 1\na\n b: foo()" => 'Function foo is missing argument $a.', "@function foo()\n @return 1\na\n b: foo(2)" => 'Function foo takes 0 arguments but 1 was passed.', "@function foo()\n @return 1\na\n b: foo($a: 1)" => "Function foo doesn't have an argument named $a.", "@function foo()\n @return 1\na\n b: foo($a: 1, $b: 2)" => "Function foo doesn't have the following arguments: $a, $b.", "@return 1" => '@return may only be used within a function.', "@if true\n @return 1" => '@return may only be used within a function.', "@mixin foo\n @return 1\n@include foo" => ['@return may only be used within a function.', 2], "@else\n a\n b: c" => ["@else must come after @if.", 1], "@if false\n@else foo" => "Invalid else directive '@else foo': expected 'if '.", "@if false\n@else if " => "Invalid else directive '@else if': expected 'if '.", "a\n $b: 12\nc\n d: $b" => 'Undefined variable: "$b".', "=foo\n $b: 12\nc\n +foo\n d: $b" => 'Undefined variable: "$b".', "c\n d: $b-foo" => 'Undefined variable: "$b-foo".', "c\n d: $b_foo" => 'Undefined variable: "$b_foo".', '@for $a from "foo" to 1' => '"foo" is not an integer.', '@for $a from 1 to "2"' => '"2" is not an integer.', '@for $a from 1 to "foo"' => '"foo" is not an integer.', '@for $a from 1 to 1.23232323232' => '1.2323232323 is not an integer.', '@for $a from 1px to 3em' => "Incompatible units: 'em' and 'px'.", '@if' => "Invalid if directive '@if': expected expression.", '@while' => "Invalid while directive '@while': expected expression.", '@debug' => "Invalid debug directive '@debug': expected expression.", %Q{@debug "a message"\n "nested message"} => "Illegal nesting: Nothing may be nested beneath debug directives.", '@error' => "Invalid error directive '@error': expected expression.", %Q{@error "a message"\n "nested message"} => "Illegal nesting: Nothing may be nested beneath error directives.", '@warn' => "Invalid warn directive '@warn': expected expression.", %Q{@warn "a message"\n "nested message"} => "Illegal nesting: Nothing may be nested beneath warn directives.", "/* foo\n bar\n baz" => "Inconsistent indentation: previous line was indented by 4 spaces, but this line was indented by 2 spaces.", '+foo(1 + 1: 2)' => 'Invalid CSS after "(1 + 1": expected comma, was ": 2)"', '+foo($var: )' => 'Invalid CSS after "($var: ": expected mixin argument, was ")"', '+foo($var: a, $var: b)' => 'Keyword argument "$var" passed more than once', '+foo($var-var: a, $var_var: b)' => 'Keyword argument "$var_var" passed more than once', '+foo($var_var: a, $var-var: b)' => 'Keyword argument "$var-var" passed more than once', "a\n b: foo(1 + 1: 2)" => 'Invalid CSS after "foo(1 + 1": expected comma, was ": 2)"', "a\n b: foo($var: )" => 'Invalid CSS after "foo($var: ": expected function argument, was ")"', "a\n b: foo($var: a, $var: b)" => 'Keyword argument "$var" passed more than once', "a\n b: foo($var-var: a, $var_var: b)" => 'Keyword argument "$var_var" passed more than once', "a\n b: foo($var_var: a, $var-var: b)" => 'Keyword argument "$var-var" passed more than once', "@if foo\n @extend .bar" => ["Extend directives may only be used within rules.", 2], "$var: true\n@while $var\n @extend .bar\n $var: false" => ["Extend directives may only be used within rules.", 3], "@for $i from 0 to 1\n @extend .bar" => ["Extend directives may only be used within rules.", 2], "@mixin foo\n @extend .bar\n@include foo" => ["Extend directives may only be used within rules.", 2], "foo %\n a: b" => ['Invalid CSS after "foo %": expected placeholder name, was ""', 1], "=foo\n @content error" => "Invalid content directive. Trailing characters found: \"error\".", "=foo\n @content\n b: c" => "Illegal nesting: Nothing may be nested beneath @content directives.", "@content" => '@content may only be used within a mixin.', "=simple\n .simple\n color: red\n+simple\n color: blue" => ['Mixin "simple" does not accept a content block.', 4], "@import \"foo\" // bar" => "Invalid CSS after \"\"foo\" \": expected media query list, was \"// bar\"", "@at-root\n a: b" => "Properties are only allowed within rules, directives, mixin includes, or other properties.", # Regression tests "a\n b:\n c\n d" => ["Illegal nesting: Only properties may be nested beneath properties.", 3], "& foo\n bar: baz\n blat: bang" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1], "a\n b: c\n& foo\n bar: baz\n blat: bang" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 3], "@" => "Invalid directive: '@'.", "$r: 20em * #ccc" => ["Cannot multiply a number with units (20em) to a color (#ccc).", 1], "$r: #ccc / 1em" => ["Cannot divide a number with units (1em) to a color (#ccc).", 1], } def teardown clean_up_sassc end def test_basic_render renders_correctly "basic", { :style => :compact } end def test_empty_render assert_equal "", render("") end def test_multiple_calls_to_render sass = Sass::Engine.new("a\n b: c") assert_equal sass.render, sass.render end def test_alternate_styles renders_correctly "expanded", { :style => :expanded } renders_correctly "compact", { :style => :compact } renders_correctly "nested", { :style => :nested } renders_correctly "compressed", { :style => :compressed } end def test_compile assert_equal "div { hello: world; }\n", Sass.compile("$who: world\ndiv\n hello: $who", :syntax => :sass, :style => :compact) assert_equal "div { hello: world; }\n", Sass.compile("$who: world; div { hello: $who }", :style => :compact) end def test_compile_file FileUtils.mkdir_p(absolutize("tmp")) open(absolutize("tmp/test_compile_file.sass"), "w") {|f| f.write("$who: world\ndiv\n hello: $who")} open(absolutize("tmp/test_compile_file.scss"), "w") {|f| f.write("$who: world; div { hello: $who }")} assert_equal "div { hello: world; }\n", Sass.compile_file(absolutize("tmp/test_compile_file.sass"), :style => :compact) assert_equal "div { hello: world; }\n", Sass.compile_file(absolutize("tmp/test_compile_file.scss"), :style => :compact) ensure FileUtils.rm_rf(absolutize("tmp")) end def test_compile_file_to_css_file FileUtils.mkdir_p(absolutize("tmp")) open(absolutize("tmp/test_compile_file.sass"), "w") {|f| f.write("$who: world\ndiv\n hello: $who")} open(absolutize("tmp/test_compile_file.scss"), "w") {|f| f.write("$who: world; div { hello: $who }")} Sass.compile_file(absolutize("tmp/test_compile_file.sass"), absolutize("tmp/test_compile_file_sass.css"), :style => :compact) Sass.compile_file(absolutize("tmp/test_compile_file.scss"), absolutize("tmp/test_compile_file_scss.css"), :style => :compact) assert_equal "div { hello: world; }\n", File.read(absolutize("tmp/test_compile_file_sass.css")) assert_equal "div { hello: world; }\n", File.read(absolutize("tmp/test_compile_file_scss.css")) ensure FileUtils.rm_rf(absolutize("tmp")) end def test_flexible_tabulation assert_equal("p {\n a: b; }\n p q {\n c: d; }\n", render("p\n a: b\n q\n c: d\n")) assert_equal("p {\n a: b; }\n p q {\n c: d; }\n", render("p\n\ta: b\n\tq\n\t\tc: d\n")) end def test_import_same_name_different_ext assert_raise_message Sass::SyntaxError, < [File.dirname(__FILE__) + '/templates/']} munge_filename options Sass::Engine.new("@import 'same_name_different_ext'", options).render end end def test_import_same_name_different_partiality assert_raise_message Sass::SyntaxError, < [File.dirname(__FILE__) + '/templates/']} munge_filename options Sass::Engine.new("@import 'same_name_different_partiality'", options).render end end EXCEPTION_MAP.each do |key, value| define_method("test_exception (#{key.inspect})") do line = 10 begin silence_warnings {Sass::Engine.new(key, :filename => FAKE_FILE_NAME, :line => line).render} rescue Sass::SyntaxError => err value = [value] unless value.is_a?(Array) assert_equal(value.first.rstrip, err.message, "Line: #{key}") assert_equal(FAKE_FILE_NAME, err.sass_filename) assert_equal((value[1] || key.split("\n").length) + line - 1, err.sass_line, "Line: #{key}") assert_match(/#{Regexp.escape(FAKE_FILE_NAME)}:[0-9]+/, err.backtrace[0], "Line: #{key}") else assert(false, "Exception not raised for\n#{key}") end end end def test_exception_line to_render = < err assert_equal(5, err.sass_line) else assert(false, "Exception not raised for '#{to_render}'!") end end def test_exception_location to_render = < FAKE_FILE_NAME, :line => (__LINE__-7)).render rescue Sass::SyntaxError => err assert_equal(FAKE_FILE_NAME, err.sass_filename) assert_equal((__LINE__-6), err.sass_line) else assert(false, "Exception not raised for '#{to_render}'!") end end def test_imported_exception [1, 2, 3, 4].each do |i| begin Sass::Engine.new("@import bork#{i}", :load_paths => [File.dirname(__FILE__) + '/templates/']).render rescue Sass::SyntaxError => err assert_equal(2, err.sass_line) assert_match(/(\/|^)bork#{i}\.sass$/, err.sass_filename) assert_hash_has(err.sass_backtrace.first, :filename => err.sass_filename, :line => err.sass_line) assert_nil(err.sass_backtrace[1][:filename]) assert_equal(1, err.sass_backtrace[1][:line]) assert_match(/(\/|^)bork#{i}\.sass:2$/, err.backtrace.first) assert_equal("(sass):1", err.backtrace[1]) else assert(false, "Exception not raised for imported template: bork#{i}") end end end def test_double_imported_exception [1, 2, 3, 4].each do |i| begin Sass::Engine.new("@import nested_bork#{i}", :load_paths => [File.dirname(__FILE__) + '/templates/']).render rescue Sass::SyntaxError => err assert_equal(2, err.sass_line) assert_match(/(\/|^)bork#{i}\.sass$/, err.sass_filename) assert_hash_has(err.sass_backtrace.first, :filename => err.sass_filename, :line => err.sass_line) assert_match(/(\/|^)nested_bork#{i}\.sass$/, err.sass_backtrace[1][:filename]) assert_equal(2, err.sass_backtrace[1][:line]) assert_nil(err.sass_backtrace[2][:filename]) assert_equal(1, err.sass_backtrace[2][:line]) assert_match(/(\/|^)bork#{i}\.sass:2$/, err.backtrace.first) assert_match(/(\/|^)nested_bork#{i}\.sass:2$/, err.backtrace[1]) assert_equal("(sass):1", err.backtrace[2]) else assert(false, "Exception not raised for imported template: bork#{i}") end end end def test_selector_tracing actual_css = render(<<-SCSS, :syntax => :scss, :trace_selectors => true) @mixin mixed { .mixed { color: red; } } .context { @include mixed; } SCSS assert_equal(< err assert_equal(2, err.sass_line) assert_equal(filename_for_test, err.sass_filename) assert_equal("error-mixin", err.sass_mixin) assert_hash_has(err.sass_backtrace.first, :line => err.sass_line, :filename => err.sass_filename, :mixin => err.sass_mixin) assert_hash_has(err.sass_backtrace[1], :line => 5, :filename => filename_for_test, :mixin => "outer-mixin") assert_hash_has(err.sass_backtrace[2], :line => 8, :filename => filename_for_test, :mixin => nil) assert_equal("#{filename_for_test}:2:in `error-mixin'", err.backtrace.first) assert_equal("#{filename_for_test}:5:in `outer-mixin'", err.backtrace[1]) assert_equal("#{filename_for_test}:8", err.backtrace[2]) end def test_mixin_callsite_exception render(< err assert_hash_has(err.sass_backtrace.first, :line => 5, :filename => filename_for_test, :mixin => "one-arg-mixin") assert_hash_has(err.sass_backtrace[1], :line => 5, :filename => filename_for_test, :mixin => "outer-mixin") assert_hash_has(err.sass_backtrace[2], :line => 8, :filename => filename_for_test, :mixin => nil) end def test_mixin_exception_cssize render(< err assert_hash_has(err.sass_backtrace.first, :line => 2, :filename => filename_for_test, :mixin => "parent-ref-mixin") assert_hash_has(err.sass_backtrace[1], :line => 6, :filename => filename_for_test, :mixin => "outer-mixin") assert_hash_has(err.sass_backtrace[2], :line => 8, :filename => filename_for_test, :mixin => nil) end def test_mixin_and_import_exception Sass::Engine.new("@import nested_mixin_bork", :load_paths => [File.dirname(__FILE__) + '/templates/']).render assert(false, "Exception not raised") rescue Sass::SyntaxError => err assert_match(/(\/|^)nested_mixin_bork\.sass$/, err.sass_backtrace.first[:filename]) assert_hash_has(err.sass_backtrace.first, :mixin => "error-mixin", :line => 4) assert_match(/(\/|^)mixin_bork\.sass$/, err.sass_backtrace[1][:filename]) assert_hash_has(err.sass_backtrace[1], :mixin => "outer-mixin", :line => 2) assert_match(/(\/|^)mixin_bork\.sass$/, err.sass_backtrace[2][:filename]) assert_hash_has(err.sass_backtrace[2], :mixin => nil, :line => 5) assert_match(/(\/|^)nested_mixin_bork\.sass$/, err.sass_backtrace[3][:filename]) assert_hash_has(err.sass_backtrace[3], :mixin => nil, :line => 6) assert_hash_has(err.sass_backtrace[4], :filename => nil, :mixin => nil, :line => 1) end def test_recursive_mixin assert_equal < filename_for_test, :load_paths => [importer], :importer => importer) assert_raise_message(Sass::SyntaxError, < filename_for_test, :load_paths => [importer], :importer => importer) assert_raise_message(Sass::SyntaxError, < true, :line => 362} render(("a\n b: c\n" * 10) + "d\n e:\n" + ("f\n g: h\n" * 10), opts) rescue Sass::SyntaxError => e assert_equal(< true) =error-mixin($a) color: $a * 1em * 1px =outer-mixin($a) +error-mixin($a) .error +outer-mixin(12) SASS rescue Sass::SyntaxError => e assert_equal(< true) .filler stuff: "stuff!" a: b .more.filler a: b SASS rescue Sass::SyntaxError => e assert_equal(< :compact, :load_paths => [File.dirname(__FILE__) + "/templates"] } assert File.exist?(sassc_file) end def test_sass_pathname_import sassc_file = sassc_path("importee") assert !File.exist?(sassc_file) renders_correctly("import", :style => :compact, :load_paths => [Pathname.new(File.dirname(__FILE__) + "/templates")]) assert File.exist?(sassc_file) end def test_import_from_global_load_paths importer = MockImporter.new importer.add_import("imported", "div{color:red}") Sass.load_paths << importer assert_equal "div {\n color: red; }\n", Sass::Engine.new('@import "imported"', :importer => importer).render ensure Sass.load_paths.clear end def test_nonexistent_import assert_raise_message(Sass::SyntaxError, < :compact, :cache => false, :load_paths => [File.dirname(__FILE__) + "/templates"], }) assert !File.exist?(sassc_path("importee")) end def test_import_in_rule assert_equal(< [File.dirname(__FILE__) + '/templates/'])) .foo #foo { background-color: #baf; } .bar { a: b; } .bar #foo { background-color: #baf; } CSS .foo @import partial .bar a: b @import partial SASS end def test_units renders_correctly "units" end def test_default_function assert_equal(< :compact)) assert_equal("#foo #bar,#baz #boom{foo:bar}\n", render("#foo #bar,\n#baz #boom\n foo: bar", :style => :compressed)) assert_equal("#foo #bar,\n#baz #boom {\n foo: bar; }\n", render("#foo #bar,,\n,#baz #boom,\n foo: bar")) assert_equal("#bip #bop {\n foo: bar; }\n", render("#bip #bop,, ,\n foo: bar")) end def test_complex_multiline_selector renders_correctly "multiline" end def test_colon_only begin render("a\n b: c", :property_syntax => :old) rescue Sass::SyntaxError => e assert_equal("Illegal property syntax: can't use new syntax when :property_syntax => :old is set.", e.message) assert_equal(2, e.sass_line) else assert(false, "SyntaxError not raised for :property_syntax => :old") end begin silence_warnings {render("a\n :b c", :property_syntax => :new)} assert_equal(2, e.sass_line) rescue Sass::SyntaxError => e assert_equal("Illegal property syntax: can't use old syntax when :property_syntax => :new is set.", e.message) else assert(false, "SyntaxError not raised for :property_syntax => :new") end end def test_pseudo_elements assert_equal(< :compact)) assert_equal("@a {\n b: c;\n}\n", render("@a\n b: c", :style => :expanded)) assert_equal("@a{b:c}\n", render("@a\n b: c", :style => :compressed)) assert_equal("@a {\n b: c;\n d: e; }\n", render("@a\n b: c\n d: e")) assert_equal("@a { b: c; d: e; }\n", render("@a\n b: c\n d: e", :style => :compact)) assert_equal("@a {\n b: c;\n d: e;\n}\n", render("@a\n b: c\n d: e", :style => :expanded)) assert_equal("@a{b:c;d:e}\n", render("@a\n b: c\n d: e", :style => :compressed)) assert_equal("@a {\n #b {\n c: d; } }\n", render("@a\n #b\n c: d")) assert_equal("@a { #b { c: d; } }\n", render("@a\n #b\n c: d", :style => :compact)) assert_equal("@a {\n #b {\n c: d;\n }\n}\n", render("@a\n #b\n c: d", :style => :expanded)) assert_equal("@a{#b{c:d}}\n", render("@a\n #b\n c: d", :style => :compressed)) assert_equal("@a {\n #b {\n a: b; }\n #b #c {\n d: e; } }\n", render("@a\n #b\n a: b\n #c\n d: e")) assert_equal("@a { #b { a: b; }\n #b #c { d: e; } }\n", render("@a\n #b\n a: b\n #c\n d: e", :style => :compact)) assert_equal("@a {\n #b {\n a: b;\n }\n #b #c {\n d: e;\n }\n}\n", render("@a\n #b\n a: b\n #c\n d: e", :style => :expanded)) assert_equal("@a{#b{a:b}#b #c{d:e}}\n", render("@a\n #b\n a: b\n #c\n d: e", :style => :compressed)) assert_equal("@a {\n #foo,\n #bar {\n b: c; } }\n", render("@a\n #foo, \n #bar\n b: c")) assert_equal("@a { #foo, #bar { b: c; } }\n", render("@a\n #foo, \n #bar\n b: c", :style => :compact)) assert_equal("@a {\n #foo,\n #bar {\n b: c;\n }\n}\n", render("@a\n #foo, \n #bar\n b: c", :style => :expanded)) assert_equal("@a{#foo,#bar{b:c}}\n", render("@a\n #foo, \n #bar\n b: c", :style => :compressed)) to_render = < :compact)) assert_equal("@a{b:c;#d{e:f}g:h}\n", render(to_render, :style => :compressed)) end def test_property_hacks assert_equal(< true, :style => :compact)) /* line 2, test_line_annotations_inline.sass */ foo bar { foo: bar; } /* line 5, test_line_annotations_inline.sass */ foo baz { blip: blop; } /* line 9, test_line_annotations_inline.sass */ floodle { flop: blop; } /* line 18, test_line_annotations_inline.sass */ bup { mix: on; } /* line 15, test_line_annotations_inline.sass */ bup mixin { moop: mup; } /* line 22, test_line_annotations_inline.sass */ bip hop, skip hop { a: b; } CSS foo bar foo: bar baz blip: blop floodle flop: blop =mxn mix: on mixin moop: mup bup +mxn bip, skip hop a: b SASS end def test_line_annotations_with_filename renders_correctly "line_numbers", :line_comments => true, :load_paths => [File.dirname(__FILE__) + "/templates"] end def test_debug_info esc_file_name = Sass::SCSS::RX.escape_ident(Sass::Util.scope("test_debug_info_inline.sass")) assert_equal(< true, :style => :compact)) @media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\000032}} foo bar { foo: bar; } @media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\000035}} foo baz { blip: blop; } @media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\000039}} floodle { flop: blop; } @media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\0000318}} bup { mix: on; } @media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\0000315}} bup mixin { moop: mup; } @media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\0000322}} bip hop, skip hop { a: b; } CSS foo bar foo: bar baz blip: blop floodle flop: blop =mxn mix: on mixin moop: mup bup +mxn bip, skip hop a: b SASS end def test_debug_info_without_filename assert_equal(< true).render) @media -sass-debug-info{filename{}line{font-family:\\000031}} foo { a: b; } CSS foo a: b SASS end def test_debug_info_with_compressed assert_equal(< true, :style => :compressed)) foo{a:b} CSS foo a: b SASS end def test_debug_info_with_line_annotations esc_file_name = Sass::SCSS::RX.escape_ident(Sass::Util.scope("test_debug_info_with_line_annotations_inline.sass")) assert_equal(< true, :line_comments => true)) @media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\000031}} foo { a: b; } CSS foo a: b SASS end def test_debug_info_in_keyframes assert_equal(< true)) @-webkit-keyframes warm { from { color: black; } to { color: red; } } CSS @-webkit-keyframes warm from color: black to color: red SASS end def test_empty_first_line assert_equal("#a {\n b: c; }\n", render("#a\n\n b: c")) end def test_escaped_rule assert_equal(":focus {\n a: b; }\n", render("\\:focus\n a: b")) assert_equal("a {\n b: c; }\n a :focus {\n d: e; }\n", render("\\a\n b: c\n \\:focus\n d: e")) end def test_cr_newline assert_equal("foo {\n a: b;\n c: d;\n e: f; }\n", render("foo\r a: b\r\n c: d\n\r e: f")) end def test_property_with_content_and_nested_props assert_equal(< :expanded } end def test_directive_style_mixins assert_equal < e assert_equal("Function plus is missing argument $var1.", e.message) end def test_function_with_extra_argument render(< e assert_equal("Function plus doesn't have an argument named $var3.", e.message) end def test_function_with_positional_and_keyword_argument render(< e assert_equal("Function plus was passed argument $var2 both by position and by name.", e.message) end def test_function_with_keyword_before_positional_argument render(< e assert_equal("Positional arguments must come before keyword arguments.", e.message) end def test_function_with_if assert_equal(< e assert_equal('Undefined variable: "$variable".', e.message) end def test_user_defined_function_can_change_global_variable assert_equal(< :compressed) foo{color:blue;/*! foo * bar */} CSS foo color: blue /*! foo * bar */ SASS end def test_loud_comment_is_evaluated assert_equal < :new)) :focus { outline: 0; } CSS :focus outline: 0 SASS end def test_pseudo_class_with_new_properties assert_equal(< :new)) p :focus { outline: 0; } CSS p :focus outline: 0 SASS end def test_nil_option assert_equal(< nil)) foo { a: b; } CSS foo a: b SASS end def test_interpolation_in_raw_functions assert_equal(< true) CSS @warn "this is a warning" SASS end end def test_warn_with_imports prefix = Sass::Util.cleanpath(File.dirname(__FILE__)).to_s expected_warning = < :compact, :load_paths => ["#{prefix}/templates"] end end def test_media_bubbling assert_equal < :compact) .foo { a: b; } @media bar { .foo { c: d; } } .foo .baz { e: f; } @media bip { .foo .baz { g: h; } } .other { i: j; } CSS .foo a: b @media bar c: d .baz e: f @media bip g: h .other i: j SASS assert_equal < :expanded) .foo { a: b; } @media bar { .foo { c: d; } } .foo .baz { e: f; } @media bip { .foo .baz { g: h; } } .other { i: j; } CSS .foo a: b @media bar c: d .baz e: f @media bip g: h .other i: j SASS end def test_double_media_bubbling assert_equal < true) /* line 5, test_line_numbers_with_dos_line_endings_inline.sass */ .foo { a: b; } CSS \r \r \r \r .foo a: b SASS end def test_variable_in_media_in_mixin assert_equal < :compressed)) .box{border-style:solid} RESULT .box border: /*color: black style: solid SOURCE end def test_compressed_comment_beneath_directive assert_equal(< :compressed)) @foo{a:b} RESULT @foo a: b /*b: c SOURCE end def test_comment_with_crazy_indentation assert_equal(< :compressed) a>b,c+d,:-moz-any(e,f,g){h:i} CSS a > b, c + d, :-moz-any(e, f, g) h: i SASS end def test_comment_like_selector assert_raise_message(Sass::SyntaxError, 'Invalid CSS after "/": expected identifier, was " foo"') {render(< original_filename, :load_paths => [importer], :syntax => :scss, :importer => importer) engine.render assert_equal original_filename, engine.options[:original_filename] assert_equal original_filename, importer.engine("imported").options[:original_filename] end def test_changing_precision old_precision = Sass::Script::Value::Number.precision begin Sass::Script::Value::Number.precision = 8 assert_equal < e assert_equal([ {:mixin => '@content', :line => 6, :filename => 'test_content_backtrace_for_perform_inline.sass'}, {:mixin => 'foo', :line => 2, :filename => 'test_content_backtrace_for_perform_inline.sass'}, {:line => 5, :filename => 'test_content_backtrace_for_perform_inline.sass'}, ], e.sass_backtrace) end def test_content_backtrace_for_cssize render(< e assert_equal([ {:mixin => '@content', :line => 6, :filename => 'test_content_backtrace_for_cssize_inline.sass'}, {:mixin => 'foo', :line => 2, :filename => 'test_content_backtrace_for_cssize_inline.sass'}, {:line => 5, :filename => 'test_content_backtrace_for_cssize_inline.sass'}, ], e.sass_backtrace) end def test_mixin_with_args_and_varargs_passed_no_var_args assert_equal < :scss) .foo { a: 1; b: 2; c: 3; } CSS @mixin three-or-more-args($a, $b, $c, $rest...) { a: $a; b: $b; c: $c; } .foo { @include three-or-more-args($a: 1, $b: 2, $c: 3); } SASS end def test_debug_inspects_sass_objects assert_warning(< :scss)} test_debug_inspects_sass_objects_inline.scss:1 DEBUG: (a: 1, b: 2) END end def test_error_throws_sass_objects assert_raise_message(Sass::SyntaxError, "(a: 1, b: 2)") {render("@error (a: 1, b: 2)")} assert_raise_message(Sass::SyntaxError, "(a: 1, b: 2)") do render("$map: (a: 1, b: 2); @error $map", :syntax => :scss) end end def test_default_arg_before_splat assert_equal < :scss) .foo-positional { a: 1; b: 2; positional-arguments: 3, 4; keyword-arguments: (); } .foo-keywords { a: true; positional-arguments: (); keyword-arguments: (c: c, d: d); } CSS @mixin foo($a: true, $b: null, $arguments...) { a: $a; b: $b; positional-arguments: inspect($arguments); keyword-arguments: inspect(keywords($arguments)); } .foo-positional { @include foo(1, 2, 3, 4); } .foo-keywords { @include foo($c: c, $d: d); } SASS end def test_keyframes assert_equal < :compressed)) x{@foo;a:b;@bar} CSS x @foo a: b @bar SASS end def test_compressed_unknown_directive_in_directive assert_equal(< :compressed)) @x{@foo;a:b;@bar} CSS @x @foo a: b @bar SASS end def test_compressed_unknown_directive_with_children_in_directive assert_equal(< :compressed)) @x{@foo{a:b}c:d;@bar{e:f}} CSS @x @foo a: b c: d @bar e: f SASS end def test_compressed_rule_in_directive assert_equal(< :compressed)) @x{foo{a:b}c:d;bar{e:f}} CSS @x foo a: b c: d bar e: f SASS end def test_import_two_css_files_issue_1806 assert_equal(< :scss, :style => :compressed)) @import url(\"foo.css\");@import url(\"bar.css\");@import url(\"baz.css\") CSS @import url("foo.css"); @import url("bar.css"); @import url("baz.css"); SASS end def test_numeric_formatting_of_integers assert_equal(< :scss, :style => :compressed)) a{near:3.0000000001;plus:3;minus:3;negative:-3} CSS a { near: (3 + 0.0000000001); plus: (3 + 0.000000000001); minus: (3 - 0.000000000001); negative: (-3 + 0.000000000001); } SASS end def test_escaped_semicolons_are_not_compressed assert_equal(<<'CSS', render(<<'SASS', :syntax => :scss, :style => :compressed)) div{color:#f00000\9\0\;} CSS div { color: #f00000\9\0\; } SASS end def test_compressed_output_of_nth_selectors assert_equal(< :scss, :style => :compressed)) :nth-of-type(2n-1),:nth-child(2n-1),:nth(2n-1),:nth-of-type(2n-1),:nth-of-type(2n-1){color:red}:nth-of-type(2n+1),:nth-child(2n+1),:nth(2n+1),:nth-of-type(2n+1),:nth-of-type(2n+1){color:red} CSS :nth-of-type(2n-1), :nth-child(2n- 1), :nth(2n -1), :nth-of-type(2n - 1), :nth-of-type( 2n - 1 ) { color: red } :nth-of-type(2n+1), :nth-child(2n+ 1), :nth(2n +1), :nth-of-type(2n + 1), :nth-of-type( 2n + 1 ) { color: red } SASS end def test_descendant_selectors_with_leading_dash assert_equal(< :scss, :style => :compressed)) a -b{color:red} CSS a -b { color: red } SASS end def test_import_with_supports_clause_interp assert_equal(< :compressed)) @import url("fallback-layout.css") supports(not (display: flex)) CSS $display-type: flex @import url("fallback-layout.css") supports(not (display: #{$display-type})) SASS end def test_import_with_supports_clause assert_equal(< :compressed)) @import url("fallback-layout.css") supports(not (display: flex)) CSS @import url("fallback-layout.css") supports(not (display: flex)) SASS end def test_compressed_commas_in_attribute_selectors assert_equal(< :compressed)) .classname[a="1, 2, 3"],.another[b="4, 5, 6"]{color:red} CSS .classname[a="1, 2, 3"], .another[b="4, 5, 6"] color: red SASS end def test_trailing_commas_in_arglists assert_equal(< :nested)) .includes { one-positional-arg: positional 1 a; two-positional-args: positional 2 a b; one-keyword-arg: keyword 1 z; two-keyword-args: keyword 2 y z; mixed-args: mixed 2 y z; } .calls { one-positional-arg: positional 1 a; two-positional-args: positional 2 a b; one-keyword-arg: keyword 1 z; two-keyword-args: keyword 2 y z; mixed-args: mixed 2 y z; } CSS =one-positional-arg($a,) one-positional-arg: positional 1 $a =two-positional-args($a, $b,) two-positional-args: positional 2 $a $b =one-keyword-arg($a: a,) one-keyword-arg: keyword 1 $a =two-keyword-args($a: a, $b: b,) two-keyword-args: keyword 2 $a $b =mixed-args($a, $b: b,) mixed-args: mixed 2 $a $b @function one-positional-arg($a) @return positional 1 $a @function two-positional-args($a, $b) @return positional 2 $a $b @function one-keyword-arg($a: a) @return keyword 1 $a @function two-keyword-args($a: a, $b: b) @return keyword 2 $a $b @function mixed-args($a, $b: b) @return mixed 2 $a $b .includes +one-positional-arg(a,) +two-positional-args(a, b,) +one-keyword-arg($a: z,) +two-keyword-args($a: y, $b: z,) +mixed-args(y, $b: z,) .calls one-positional-arg: one-positional-arg(a) two-positional-args: two-positional-args(a, b) one-keyword-arg: one-keyword-arg($a: z) two-keyword-args: two-keyword-args($a: y, $b: z) mixed-args: mixed-args(y, $b: z) SASS end private def assert_hash_has(hash, expected) expected.each do |k, v| if v.nil? assert_nil(hash[k]) else assert_equal(v, hash[k]) end end end def assert_renders_encoded(css, sass) result = render(sass) assert_equal css.encoding, result.encoding assert_equal css, result end def render(sass, options = {}) munge_filename options options[:importer] ||= MockImporter.new Sass::Engine.new(sass, options).render end def renders_correctly(name, options={}) sass_file = load_file(name, "sass") css_file = load_file(name, "css") options[:filename] ||= filename(name, "sass") options[:syntax] ||= :sass options[:css_filename] ||= filename(name, "css") css_result = Sass::Engine.new(sass_file, options).render assert_equal css_file, css_result end def load_file(name, type = "sass") @result = '' File.new(filename(name, type)).each_line { |l| @result += l } @result end def filename(name, type) path = File.dirname(__FILE__) + "/#{type == 'sass' ? 'templates' : 'results'}/#{name}.#{type}" Sass::Util.cleanpath(path).to_s end def sassc_path(template) sassc_path = File.join(File.dirname(__FILE__) + "/templates/#{template}.sass") engine = Sass::Engine.new("", :filename => sassc_path, :importer => Sass::Importers::Filesystem.new(".")) key = engine.send(:sassc_key) File.join(engine.options[:cache_location], key) end end