psychgus 1.2.0 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,11 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
11
 
@@ -26,18 +15,18 @@ require 'tempfile'
26
15
 
27
16
  class FlowStyler
28
17
  include Psychgus::Styler
29
-
18
+
30
19
  def style_mapping(sniffer,node)
31
20
  node.style = Psychgus::MAPPING_FLOW if sniffer.level >= 4
32
21
  end
33
-
22
+
34
23
  def style_sequence(sniffer,node)
35
24
  node.style = Psychgus::SEQUENCE_FLOW if sniffer.level >= 4
36
25
  end
37
26
  end
38
27
 
39
28
  class PsychgusTest < PsychgusTester
40
- EXPECTED_BURGERS = <<-EOY
29
+ EXPECTED_BURGERS = <<-YAML
41
30
  ---
42
31
  Burgers:
43
32
  Classic: {Sauce: [Ketchup, Mustard], Cheese: American, Bun: Sesame Seed}
@@ -47,14 +36,14 @@ Toppings:
47
36
  - Mushrooms
48
37
  - [Lettuce, Onions, Pickles, Tomatoes]
49
38
  - [[Ketchup, Mustard], [Salt, Pepper]]
50
- EOY
51
-
52
- def setup()
53
- @flow_styler = FlowStyler.new()
39
+ YAML
40
+
41
+ def setup
42
+ @flow_styler = FlowStyler.new
54
43
  end
55
-
56
- def test_alias()
57
- expected = <<-EOY
44
+
45
+ def test_alias
46
+ expected = <<-YAML
58
47
  |---
59
48
  |Dolphins:
60
49
  | Common: {Length: "~2.5m", Weight: "~235kg"}
@@ -64,43 +53,56 @@ Toppings:
64
53
  |Popular:
65
54
  |- {Length: "~4m", Weight: "~300kg"}
66
55
  |- {Length: "~7m", Weight: "~3600kg"}
67
- EOY
56
+ YAML
68
57
  expected = self.class.lstrip_pipe(expected)
69
-
58
+
70
59
  assert_equal expected,DOLPHINS_DATA.to_yaml(deref_aliases: true,stylers: @flow_styler)
71
60
  end
72
-
73
- def test_dump()
61
+
62
+ def test_dump
74
63
  assert_equal EXPECTED_BURGERS,Psychgus.dump(BURGERS_DATA,stylers: @flow_styler)
75
64
  assert_equal EXPECTED_BURGERS,Psychgus.dump_stream(BURGERS_DATA,stylers: @flow_styler)
76
65
  assert_equal EXPECTED_BURGERS,BURGERS_DATA.to_yaml(stylers: @flow_styler)
77
66
  end
78
-
67
+
79
68
  # Execute "rake test_all" if you update Psychgus.dump_file()/load_file()
80
- def test_file()
69
+ def test_file
81
70
  if !TEST_ALL
82
71
  skip(TEST_ALL_SKIP_MSG)
83
72
  return # Justin Case
84
73
  end
85
-
74
+
86
75
  Tempfile.create(['Psychgus','.yaml']) do |file|
87
76
  puts "Testing #{self.class.name} w/ temp file: #{file.path}"
88
-
89
- Psychgus.dump_file(file,BURGERS_DATA,stylers: @flow_styler)
90
-
91
- file.rewind()
92
- lines = file.readlines().join()
77
+
78
+ Psychgus.dump_file(file,BURGERS_DATA,
79
+ mode: File::CREAT | File::RDWR,
80
+ opt: {textmode: true},
81
+ #perm: 644, # Unix only
82
+ stylers: @flow_styler,
83
+ )
84
+
85
+ file.rewind
86
+
87
+ lines = file.readlines.join
93
88
  assert_equal EXPECTED_BURGERS,lines
94
-
95
- file.rewind()
89
+
90
+ file.rewind
91
+ file.close
92
+
96
93
  data = Psych.load_file(file)
97
94
  refute_equal false,data
95
+ refute_equal nil,data
96
+
97
+ data = Psychgus.parse_file(file)
98
+ refute_equal false,data
99
+ refute_equal nil,data
98
100
  end
99
101
  end
100
-
101
- def test_indent()
102
+
103
+ def test_indent
102
104
  # Indent of 3 spaces
103
- expected = <<-EOY
105
+ expected = <<-YAML
104
106
  |---
105
107
  |Burgers:
106
108
  | Classic: {Sauce: [Ketchup, Mustard], Cheese: American, Bun: Sesame Seed}
@@ -110,32 +112,49 @@ Toppings:
110
112
  |- Mushrooms
111
113
  |- [Lettuce, Onions, Pickles, Tomatoes]
112
114
  |- [[Ketchup, Mustard], [Salt, Pepper]]
113
- EOY
115
+ YAML
114
116
  expected = self.class.lstrip_pipe(expected)
115
-
117
+
118
+ # rubocop:disable Style/HashSyntax
116
119
  assert_equal expected,BURGERS_DATA.to_yaml(indent: 3,stylers: @flow_styler)
117
- assert_equal expected,BURGERS_DATA.to_yaml({:indent=>3,:stylers=>@flow_styler})
120
+ assert_equal expected,BURGERS_DATA.to_yaml(**{:indent => 3,:stylers => @flow_styler})
118
121
  assert_equal expected,BURGERS_DATA.to_yaml(indentation: 3,stylers: @flow_styler)
119
- assert_equal expected,BURGERS_DATA.to_yaml({:indentation=>3,:stylers=>@flow_styler})
122
+ assert_equal expected,BURGERS_DATA.to_yaml(**{:indentation => 3,:stylers => @flow_styler})
123
+ # rubocop:enable all
120
124
  end
121
-
122
- def test_node_consts()
125
+
126
+ def test_node_consts
127
+ assert_equal Psych::Nodes::Mapping::ANY,Psychgus::MAPPING_ANY
128
+ assert_equal Psych::Nodes::Mapping::BLOCK,Psychgus::MAPPING_BLOCK
123
129
  assert_equal Psych::Nodes::Mapping::FLOW,Psychgus::MAPPING_FLOW
130
+
131
+ assert_equal Psych::Nodes::Scalar::ANY,Psychgus::SCALAR_ANY
132
+ assert_equal Psych::Nodes::Scalar::PLAIN,Psychgus::SCALAR_PLAIN
133
+ assert_equal Psych::Nodes::Scalar::SINGLE_QUOTED,Psychgus::SCALAR_SINGLE_QUOTED
134
+ assert_equal Psych::Nodes::Scalar::DOUBLE_QUOTED,Psychgus::SCALAR_DOUBLE_QUOTED
135
+ assert_equal Psych::Nodes::Scalar::LITERAL,Psychgus::SCALAR_LITERAL
124
136
  assert_equal Psych::Nodes::Scalar::FOLDED,Psychgus::SCALAR_FOLDED
137
+
138
+ assert_equal Psych::Nodes::Sequence::ANY,Psychgus::SEQUENCE_ANY
139
+ assert_equal Psych::Nodes::Sequence::BLOCK,Psychgus::SEQUENCE_BLOCK
125
140
  assert_equal Psych::Nodes::Sequence::FLOW,Psychgus::SEQUENCE_FLOW
141
+
142
+ assert_equal Psych::Nodes::Stream::ANY,Psychgus::STREAM_ANY
126
143
  assert_equal Psych::Nodes::Stream::UTF8,Psychgus::STREAM_UTF8
144
+ assert_equal Psych::Nodes::Stream::UTF16LE,Psychgus::STREAM_UTF16LE
145
+ assert_equal Psych::Nodes::Stream::UTF16BE,Psychgus::STREAM_UTF16BE
127
146
  end
128
-
129
- def test_parse()
147
+
148
+ def test_parse
130
149
  parser = Psychgus.parser(stylers: @flow_styler)
131
150
  parser.parse(BURGERS_YAML)
132
- yaml = "---\n" + parser.handler.root.to_yaml()
151
+ yaml = "---\n" + parser.handler.root.to_yaml
133
152
  assert_equal EXPECTED_BURGERS,yaml
134
-
153
+
135
154
  node = Psychgus.parse(BURGERS_YAML,stylers: @flow_styler)
136
155
  refute_equal false,node
137
-
138
- yaml = Psychgus.parse_stream(BURGERS_YAML,stylers: @flow_styler).to_yaml()
156
+
157
+ yaml = Psychgus.parse_stream(BURGERS_YAML,stylers: @flow_styler).to_yaml
139
158
  yaml = "---\n#{yaml}"
140
159
  assert_equal EXPECTED_BURGERS,yaml
141
160
  end
@@ -1,23 +1,11 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  #--
6
5
  # This file is part of Psychgus.
7
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
8
- #
9
- # Psychgus is free software: you can redistribute it and/or modify
10
- # it under the terms of the GNU Lesser General Public License as published by
11
- # the Free Software Foundation, either version 3 of the License, or
12
- # (at your option) any later version.
13
- #
14
- # Psychgus is distributed in the hope that it will be useful,
15
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
- # GNU Lesser General Public License for more details.
18
- #
19
- # You should have received a copy of the GNU Lesser General Public License
20
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
21
9
  #++
22
10
 
23
11
 
@@ -30,13 +18,13 @@ class PsychgusTester < Minitest::Test
30
18
  # If true, will...
31
19
  # - Run tests that create temp file(s).
32
20
  # - I don't like creating temp file(s) every time I run tests (which is a lot).
33
- #
21
+ #
34
22
  # To do this, execute:
35
23
  # rake test_all
36
- TEST_ALL = (ENV['PSYCHGUS_TEST'].to_s().strip().casecmp('all') == 0)
37
- TEST_ALL_SKIP_MSG = %q(Execute "rake test_all" for this test)
38
-
39
- BURGERS_YAML = <<-EOY.freeze()
24
+ TEST_ALL = (ENV['PSYCHGUS_TEST'].to_s.strip.casecmp('all') == 0)
25
+ TEST_ALL_SKIP_MSG = 'Execute "rake test_all" for this test'
26
+
27
+ BURGERS_YAML = <<-YAML
40
28
  Burgers:
41
29
  Classic:
42
30
  Sauce: [Ketchup,Mustard]
@@ -54,10 +42,10 @@ Toppings:
54
42
  - Mushrooms
55
43
  - [Lettuce, Onions, Pickles, Tomatoes]
56
44
  - [[Ketchup,Mustard], [Salt,Pepper]]
57
- EOY
58
- BURGERS_DATA = Psych.load(BURGERS_YAML).freeze()
59
-
60
- COURSES_YAML = <<-EOY.freeze()
45
+ YAML
46
+ BURGERS_DATA = Psych.load(BURGERS_YAML).freeze
47
+
48
+ COURSES_YAML = <<-YAML
61
49
  Courses:
62
50
  COSC: [470,'Computer Science']
63
51
  MUSC: [340,'Music']
@@ -67,10 +55,10 @@ Schedule:
67
55
  - {Course: MUSC,Time: '10:30'}
68
56
  - {Course: ARTS,Time: '15:10'}
69
57
  - {Course: COSC,Time: '13:10'}
70
- EOY
71
- COURSES_DATA = Psych.load(COURSES_YAML).freeze()
72
-
73
- DOLPHINS_YAML = <<-EOY.freeze()
58
+ YAML
59
+ COURSES_DATA = Psych.load(COURSES_YAML).freeze
60
+
61
+ DOLPHINS_YAML = <<-YAML
74
62
  Dolphins:
75
63
  Common: &com {Length: ~2.5m, Weight: ~235kg}
76
64
  Bottlenose: &bot {Length: ~4m, Weight: ~300kg}
@@ -79,9 +67,13 @@ Dolphins:
79
67
  Popular:
80
68
  - *bot
81
69
  - *orc
82
- EOY
83
- DOLPHINS_DATA = Psych.load(DOLPHINS_YAML).freeze()
84
-
70
+ YAML
71
+ # Psych v4+ uses safe_load() by default for load(),
72
+ # so use unsafe_load() to have aliases turned on.
73
+ # Don't do 'aliases: true' because that doesn't exist
74
+ # in older versions of Psych.
75
+ DOLPHINS_DATA = Psych.unsafe_load(DOLPHINS_YAML).freeze
76
+
85
77
  # This is for "<<-" heredoc
86
78
  # - Purposely not using "<<~" (tilde) for older Ruby versions
87
79
  def self.lstrip_pipe(str)
data/test/sniffer_test.rb CHANGED
@@ -1,22 +1,11 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
11
 
@@ -25,18 +14,18 @@ require 'psychgus_tester'
25
14
  require 'stringio'
26
15
 
27
16
  class SnifferTest < PsychgusTester
28
- def setup()
17
+ def setup
29
18
  end
30
-
19
+
31
20
  def assert_hierarchy(*data,expected)
32
21
  expected = self.class.lstrip_pipe(expected)
33
22
  hierarchy = Psychgus.hierarchy(*data,verbose: true)
34
-
23
+
35
24
  assert_equal expected,hierarchy
36
25
  end
37
-
38
- def test_multi_doc()
39
- assert_hierarchy(BURGERS_DATA,COURSES_DATA,DOLPHINS_DATA,<<-EOH
26
+
27
+ def test_multi_doc
28
+ assert_hierarchy(BURGERS_DATA,COURSES_DATA,DOLPHINS_DATA,<<-HIER)
40
29
  |(1:1):Psych::Nodes::Stream - <root:(0:0)::(:1)>
41
30
  |(1:1):Psych::Nodes::Document - <stream:(1:1)::(:1)>
42
31
  |(1:1):Psych::Nodes::Mapping - <doc:(1:1)::(:1)>
@@ -153,12 +142,11 @@ class SnifferTest < PsychgusTester
153
142
  | (3:1):Psych::Nodes::Sequence - <Popular:(2:2):value:(:1)>
154
143
  | (4:1):Psych::Nodes::Alias - <seq:(3:1)::(:1)>
155
144
  | (4:2):Psych::Nodes::Alias - <seq:(3:1)::(:2)>
156
- EOH
157
- )
145
+ HIER
158
146
  end
159
-
160
- def test_single_docs()
161
- assert_hierarchy(BURGERS_DATA,<<-EOH
147
+
148
+ def test_single_docs
149
+ assert_hierarchy(BURGERS_DATA,<<-HIER)
162
150
  |(1:1):Psych::Nodes::Stream - <root:(0:0)::(:1)>
163
151
  |(1:1):Psych::Nodes::Document - <stream:(1:1)::(:1)>
164
152
  |(1:1):Psych::Nodes::Mapping - <doc:(1:1)::(:1)>
@@ -205,10 +193,9 @@ class SnifferTest < PsychgusTester
205
193
  | (5:2):Psych::Nodes::Sequence - <seq:(4:3)::(:2)>
206
194
  | (6:1):Salt - <seq:(5:2)::(:1)>
207
195
  | (6:2):Pepper - <seq:(5:2)::(:2)>
208
- EOH
209
- )
210
-
211
- assert_hierarchy(COURSES_DATA,<<-EOH
196
+ HIER
197
+
198
+ assert_hierarchy(COURSES_DATA,<<-HIER)
212
199
  |(1:1):Psych::Nodes::Stream - <root:(0:0)::(:1)>
213
200
  |(1:1):Psych::Nodes::Document - <stream:(1:1)::(:1)>
214
201
  |(1:1):Psych::Nodes::Mapping - <doc:(1:1)::(:1)>
@@ -248,7 +235,6 @@ class SnifferTest < PsychgusTester
248
235
  | (6:1):COSC - <Course:(5:1):value:(:1)>
249
236
  | (5:2):Time - <map:(4:4):key:(:2)>
250
237
  | (6:1):13:10 - <Time:(5:2):value:(:1)>
251
- EOH
252
- )
238
+ HIER
253
239
  end
254
240
  end
data/test/styler_test.rb CHANGED
@@ -1,22 +1,11 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
11
 
@@ -24,45 +13,45 @@ require 'psychgus_tester'
24
13
 
25
14
  class MyStyler
26
15
  include Psychgus::Styler
27
-
16
+
28
17
  def style(sniffer,node)
29
18
  end
30
-
19
+
31
20
  def style_alias(sniffer,node)
32
21
  end
33
-
22
+
34
23
  def style_mapping(sniffer,node)
35
24
  parent = sniffer.parent
36
-
37
- if !parent.nil?()
25
+
26
+ if !parent.nil?
38
27
  # BBQ
39
28
  node.style = Psychgus::MAPPING_FLOW if parent.node_of?(:scalar) && parent.value.casecmp('BBQ') == 0
40
-
29
+
41
30
  # Fancy
42
31
  node.style = Psychgus::MAPPING_FLOW if parent.level == 4 && parent.position == 3
43
32
  end
44
33
  end
45
-
34
+
46
35
  def style_scalar(sniffer,node)
47
36
  node.style = Psychgus::SCALAR_SINGLE_QUOTED if node.value.casecmp('Mushrooms') == 0
48
37
  node.value = 'Spinach' if node.value.casecmp('Lettuce') == 0
49
38
  end
50
-
39
+
51
40
  def style_sequence(sniffer,node)
52
41
  node.style = Psychgus::SEQUENCE_FLOW if sniffer.level >= 3
53
-
42
+
54
43
  # Burgers=>Classic=>Sauce and Mushrooms
55
44
  node.style = Psychgus::SEQUENCE_BLOCK if sniffer.position == 1
56
45
  end
57
46
  end
58
47
 
59
48
  class StylerTest < PsychgusTester
60
- def setup()
61
- @styler = MyStyler.new()
49
+ def setup
50
+ @styler = MyStyler.new
62
51
  end
63
-
64
- def test_styler()
65
- expected = <<-EOY
52
+
53
+ def test_styler
54
+ expected = <<-YAML
66
55
  |---
67
56
  |Burgers:
68
57
  | Classic:
@@ -77,9 +66,9 @@ class StylerTest < PsychgusTester
77
66
  |- 'Mushrooms'
78
67
  |- [Spinach, Onions, Pickles, Tomatoes]
79
68
  |- [[Ketchup, Mustard], [Salt, Pepper]]
80
- EOY
69
+ YAML
81
70
  expected = self.class.lstrip_pipe(expected)
82
-
71
+
83
72
  assert_equal expected,BURGERS_DATA.to_yaml(stylers: @styler)
84
73
  end
85
74
  end