This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
forked from pitr/angular-rails-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompact_javascript_escape_test.rb
58 lines (48 loc) · 1.71 KB
/
compact_javascript_escape_test.rb
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
require 'test_helper'
require 'angular-rails-templates/compact_javascript_escape'
describe AngularRailsTemplates::CompactJavaScriptEscape do
let(:instance) do
Class.new { include AngularRailsTemplates::CompactJavaScriptEscape }.new
end
it "responds to :escape_javascript" do
instance.must_respond_to :escape_javascript
end
describe "#escape_javascript" do
it "returns strings" do
instance.escape_javascript("hello").must_be_kind_of String
instance.escape_javascript(""). must_be_kind_of String
instance.escape_javascript(nil). must_be_kind_of String
end
it "uses double quotes to wrap strings without quotes" do
str = instance.escape_javascript("hello")
str[0].must_equal str[-1]
str[0].must_equal %(")
end
it "uses double quotes to wrap strings with many single quotes" do
str = instance.escape_javascript(%{'hello'})
str[0].must_equal str[-1]
str[0].must_equal %(")
end
it "uses single quotes to wrap strings with many double quotes" do
str = instance.escape_javascript(%{"hello"})
str[0].must_equal str[-1]
str[0].must_equal %(')
end
it "escapes single quotes when double quotes are used to wrap strings" do
str = instance.escape_javascript(%{'h'e"l'lo'})
str[0].must_equal %(")
str.must_match %{\\"}
str.wont_match %{\\'}
end
it "escapes double quotes when single quotes are used to wrap strings" do
str = instance.escape_javascript(%{"h"e'l"lo"})
str[0].must_equal %(')
str.must_match %{\\'}
str.wont_match %{\\"}
end
it "escapes backslashes" do
str = instance.escape_javascript(%{a\\z})
str.must_match %{a\\\\z}
end
end
end