prettyp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Prettyp::FormatterRegistry do
4
+
5
+ context 'default formatters' do
6
+ subject { described_class.new }
7
+
8
+ let(:formatter_class) { Prettyp::Formatter::XML::Xmllint }
9
+
10
+ it 'should select a formatter we know' do
11
+ expect(formatter_class).to receive(:check_requirement).and_return(true)
12
+ expect(subject.formatter_for('xml')).to be formatter_class
13
+ end
14
+
15
+ end
16
+
17
+ context 'providing the formatters' do
18
+
19
+ subject { described_class.new(formatters) }
20
+
21
+ let(:formatters) { [ formatter ] }
22
+ let(:formatter) { class_double(Prettyp::Formatter::BaseFormatter, check_requirement: check_requirement, languages: ['random'] ) }
23
+
24
+ context 'formatter passes check_requirement' do
25
+ let(:check_requirement) { true }
26
+
27
+ it 'will select our formatter' do
28
+ expect(subject.formatter_for('random')).to be formatter
29
+ end
30
+ end
31
+
32
+ context 'formatter fails check_requirement' do
33
+ let(:check_requirement) { false }
34
+
35
+ it 'will not select our formatter' do
36
+ expect(subject.formatter_for('random')).to be nil
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Prettyp::FormatterService do
4
+
5
+ let(:formatter_registry) { Prettyp::FormatterRegistry.new }
6
+ let(:language_detection) { Prettyp::LanguageDetectionService.new }
7
+
8
+ let(:formatter) { double('formatter', format: 'XML!')}
9
+ let(:formatter_class) { class_double(Prettyp::Formatter::BaseFormatter) }
10
+
11
+ subject { described_class.new(formatter_registry: formatter_registry, language_detection: language_detection) }
12
+
13
+ context 'from a file' do
14
+ it 'detects a language' do
15
+ expect(language_detection).to receive(:detect_from_file).and_return('xml')
16
+ expect(formatter_registry).to receive(:formatter_for).with('xml').and_return(formatter_class)
17
+ expect(formatter_class).to receive(:new).and_return(formatter)
18
+
19
+ expect(subject.format_from_file('no_file')).to eq('XML!')
20
+ end
21
+ end
22
+
23
+ context 'using stdin' do
24
+ it 'detects a language' do
25
+ expect($stdin).to receive(:read).and_return('test')
26
+ expect(language_detection).to receive(:detect_from_input).and_return('xml')
27
+ expect(formatter_registry).to receive(:formatter_for).with('xml').and_return(formatter_class)
28
+ expect(formatter_class).to receive(:new).and_return(formatter)
29
+
30
+ subject.format_from_stdin
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Prettyp::Formatter::XML::Xmllint do
4
+
5
+ subject { described_class.new }
6
+
7
+ it 'can be used' do
8
+ expect(subject.check_requirement).to be true
9
+ end
10
+
11
+ context 'format' do
12
+
13
+ let(:format) { subject.format(input, :xml) }
14
+
15
+ context 'invalid input' do
16
+ let(:input) { 'abcd' }
17
+ it 'raises a format error' do
18
+ expect{ format }.to raise_error Prettyp::Formatter::FormatError
19
+ end
20
+ end
21
+
22
+ context 'valid input' do
23
+ let(:input) do
24
+ %|<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>|
25
+ end
26
+
27
+ it 'returns a formatted document' do
28
+ expect(format.split("\n").size).to be >= 2
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Prettyp::LanguageDetectionService do
4
+
5
+ before(:each) do
6
+ FakeFS.activate!
7
+ FileUtils.mkdir '/tmp'
8
+ end
9
+
10
+ after(:each) do
11
+ FakeFS::FileSystem.clear
12
+ FakeFS.deactivate!
13
+ end
14
+
15
+ subject { described_class.new }
16
+
17
+ let(:file) do
18
+ File.open(file_name, 'w+') { |f| f.write file_contents }
19
+ f = File.new(file_name)
20
+ end
21
+
22
+ context 'xml' do
23
+ let(:file_contents) { %|<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>| }
24
+
25
+ context 'file extension' do
26
+ let(:file_name) { '/tmp/prettyp.xml' }
27
+ it 'should detect the file as XML' do
28
+ expect(subject.detect_from_file(file)).to eq(:xml)
29
+ end
30
+ end
31
+
32
+ context 'no file extension' do
33
+ let(:file_name) { '/tmp/prettyp' }
34
+ it 'should detect the file as XML' do
35
+ expect(subject.detect_from_file(file)).to eq(:xml)
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'json' do
41
+ let(:file_contents) { %|{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }}| }
42
+
43
+ context 'file extension' do
44
+ let(:file_name) { '/tmp/prettyp.json' }
45
+
46
+ it 'should detect the file as JSON' do
47
+ expect(subject.detect_from_file(file)).to eq(:json)
48
+ end
49
+ end
50
+
51
+ context 'no file extension' do
52
+ let(:file_name) { '/tmp/prettyp' }
53
+
54
+ it 'should detect the file as JSON' do
55
+ expect(subject.detect_from_file(file)).to eq(:json)
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'prettyp'
5
+
6
+ require 'pry'
7
+
8
+ require 'fakefs/safe'
9
+ require 'fakefs/spec_helpers'
10
+
11
+ RSpec.configure do |config|
12
+ # include FakeFS::SpecHelpers
13
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prettyp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: github-linguist
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Pretty Print using popular tools and many languages
84
+ email:
85
+ - jacob@dekz.net
86
+ executables:
87
+ - prettyp
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/prettyp
98
+ - lib/prettyp.rb
99
+ - lib/prettyp/formatter/base_formatter.rb
100
+ - lib/prettyp/formatter/html.rb
101
+ - lib/prettyp/formatter/html/tidy.rb
102
+ - lib/prettyp/formatter/json.rb
103
+ - lib/prettyp/formatter/json/python.rb
104
+ - lib/prettyp/formatter/ruby.rb
105
+ - lib/prettyp/formatter/xml.rb
106
+ - lib/prettyp/formatter/xml/xmllint.rb
107
+ - lib/prettyp/formatter_registry.rb
108
+ - lib/prettyp/formatter_service.rb
109
+ - lib/prettyp/formatters.rb
110
+ - lib/prettyp/language_detection_service.rb
111
+ - lib/prettyp/logger.rb
112
+ - lib/prettyp/version.rb
113
+ - prettyp.gemspec
114
+ - spec/fixtures/samples.json
115
+ - spec/formatter_registry_spec.rb
116
+ - spec/formatter_service_spec.rb
117
+ - spec/formatters/xml/xmllint_spec.rb
118
+ - spec/language_detection_service_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Pretty Print language formatter
144
+ test_files:
145
+ - spec/fixtures/samples.json
146
+ - spec/formatter_registry_spec.rb
147
+ - spec/formatter_service_spec.rb
148
+ - spec/formatters/xml/xmllint_spec.rb
149
+ - spec/language_detection_service_spec.rb
150
+ - spec/spec_helper.rb