rundoc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ class AppendFileTest < Test::Unit::TestCase
4
+
5
+ def test_appends_to_a_file
6
+ Dir.mktmpdir do |dir|
7
+ Dir.chdir(dir) do
8
+
9
+ file = "foo.rb"
10
+ `echo 'foo' >> #{file}`
11
+
12
+ cc = Rundoc::CodeCommand::FileCommand::Append.new(file)
13
+ cc << "bar"
14
+ cc.call
15
+
16
+ result = File.read(file)
17
+
18
+ assert_match /foo/, result
19
+ assert_match /bar/, result
20
+
21
+ cc = Rundoc::CodeCommand::FileCommand::Append.new(file)
22
+ cc << "baz"
23
+ cc.call
24
+
25
+ actual = File.read(file)
26
+ expected = "foo\nbar\nbaz\n"
27
+ assert_equal expected, actual
28
+ end
29
+ end
30
+ end
31
+
32
+ def test_appends_to_a_file_at_line_number
33
+ Dir.mktmpdir do |dir|
34
+ Dir.chdir(dir) do
35
+
36
+ contents = <<-CONTENTS
37
+ source 'https://rubygems.org'
38
+ gem 'rails', '4.0.0'
39
+ CONTENTS
40
+
41
+ file = "foo.rb"
42
+ line = 2
43
+ `echo '#{contents}' >> #{file}`
44
+
45
+ cc = Rundoc::CodeCommand::FileCommand::Append.new("#{file}##{line}")
46
+ cc << "gem 'pg'"
47
+ cc.call
48
+
49
+ expected = "source https://rubygems.org\ngem 'pg'\ngem rails, 4.0.0\n\n"
50
+ assert_equal expected, File.read(file)
51
+ end
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class BashTest < Test::Unit::TestCase
4
+
5
+ def test_bash_returns_cd
6
+ original_dir = `pwd`
7
+ Rundoc::CodeCommand::Bash.new("cd ..").call
8
+ now_dir = `pwd`
9
+ refute_equal original_dir, now_dir
10
+ ensure
11
+ Dir.chdir(original_dir.strip)
12
+ end
13
+
14
+
15
+ def test_bash_shells_and_shows_correctly
16
+ ["pwd", "ls"].each do |command|
17
+ bash = Rundoc::CodeCommand::Bash.new(command)
18
+ assert_equal "$ #{command}", bash.to_md
19
+ assert_equal `#{command}`, bash.call
20
+ end
21
+ end
22
+
23
+ def test_stdin
24
+ command = "tail -n 2"
25
+ bash = Rundoc::CodeCommand::Bash.new(command)
26
+ bash << "foo\nbar\nbaz\n"
27
+ assert_equal "bar\nbaz\n", bash.call
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class PipeTest < Test::Unit::TestCase
4
+
5
+ def test_pipe
6
+ pipe_cmd = "tail -n 2"
7
+ cmd = "ls"
8
+ out = `#{cmd}`
9
+ pipe = Rundoc::CodeCommand::Pipe.new(pipe_cmd)
10
+ actual = pipe.call(commands: [{command: cmd, output: out}])
11
+
12
+ expected = `#{cmd} | #{pipe_cmd}`
13
+ assert_equal expected, actual
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class RemoveContentsTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @gemfile = <<-RUBY
7
+
8
+ source 'https://rubygems.org'
9
+
10
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
11
+ gem 'rails', '4.0.0'
12
+
13
+ gem 'sqlite3'
14
+ RUBY
15
+ end
16
+
17
+ def test_appends_to_a_file
18
+ Dir.mktmpdir do |dir|
19
+ Dir.chdir(dir) do
20
+
21
+ @file = "foo.rb"
22
+ `echo "#{@gemfile}" >> #{@file}`
23
+
24
+ assert_match /sqlite3/, File.read(@file)
25
+
26
+ cc = Rundoc::CodeCommand::FileCommand::Remove.new(@file)
27
+ cc << "gem 'sqlite3'"
28
+ cc.call
29
+
30
+ result = File.read(@file)
31
+ refute_match /sqlite3/, File.read(@file)
32
+
33
+ env = {}
34
+ env[:commands] = []
35
+ env[:before] = "```ruby"
36
+ cc.to_md(env)
37
+
38
+ assert_equal "In file `foo.rb` remove:\n\n```ruby", env[:before]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class CodeSectionTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_does_not_render_if_all_contents_hidden
9
+ contents = <<-RUBY
10
+ sup
11
+
12
+ ```
13
+ :::- $ mkdir foo
14
+ ```
15
+
16
+ yo
17
+ RUBY
18
+
19
+
20
+ Dir.mktmpdir do |dir|
21
+ Dir.chdir(dir) do
22
+ match = contents.match(Rundoc::Parser::CODEBLOCK_REGEX)
23
+ result = Rundoc::CodeSection.new(match, keyword: ":::").render
24
+ assert_equal "", result
25
+ end
26
+ end
27
+ end
28
+
29
+
30
+ def test_no_code
31
+ contents = <<-RUBY
32
+ ```ruby
33
+ gem 'sqlite3'
34
+ ```
35
+ RUBY
36
+
37
+ match = contents.match(Rundoc::Parser::CODEBLOCK_REGEX)
38
+ result = Rundoc::CodeSection.new(match, keyword: ":::").render
39
+ assert_equal contents, result
40
+ end
41
+
42
+ end
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ class ParserTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_parse_bash
9
+ contents = <<-RUBY
10
+ sup
11
+
12
+ ```
13
+ ::: $ mkdir foo
14
+ :::= $ ls
15
+ ```
16
+
17
+ yo
18
+ RUBY
19
+
20
+
21
+ Dir.mktmpdir do |dir|
22
+ Dir.chdir(dir) do
23
+ expected = "sup\n\n```\n$ mkdir foo\n$ ls\nfoo\n```\n\nyo\n"
24
+ parsed = Rundoc::Parser.new(contents)
25
+ actual = parsed.to_md
26
+ assert_equal expected, actual
27
+
28
+ parsed = Rundoc::Parser.new("\n```\n:::= $ ls\n```\n")
29
+ actual = parsed.to_md
30
+ expected = "\n```\n$ ls\nfoo\n```\n"
31
+ assert_equal expected, actual
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+ def test_parse_write_commands
38
+ contents = <<-RUBY
39
+ sup
40
+
41
+ ```
42
+ :::= write foo/code.rb
43
+ a = 1 + 1
44
+ b = a * 2
45
+ ```
46
+ yo
47
+ RUBY
48
+
49
+ Dir.mktmpdir do |dir|
50
+ Dir.chdir(dir) do
51
+
52
+ expected = "sup\n\nIn file `foo/code.rb` write:\n\n```\na = 1 + 1\nb = a * 2\n```\nyo\n"
53
+ parsed = Rundoc::Parser.new(contents)
54
+ actual = parsed.to_md
55
+ assert_equal expected, actual
56
+ end
57
+ end
58
+
59
+
60
+ contents = <<-RUBY
61
+
62
+ ```
63
+ :::= write foo/newb.rb
64
+ puts 'hello world'
65
+ :::= $ cat foo/newb.rb
66
+ ```
67
+ RUBY
68
+
69
+ Dir.mktmpdir do |dir|
70
+ Dir.chdir(dir) do
71
+
72
+ expected = "\nIn file `foo/newb.rb` write:\n\n```\nputs 'hello world'\n$ cat foo/newb.rb\nputs 'hello world'\n```\n"
73
+ parsed = Rundoc::Parser.new(contents)
74
+ actual = parsed.to_md
75
+ assert_equal expected, actual
76
+ end
77
+ end
78
+ end
79
+
80
+ def test_irb
81
+
82
+ contents = <<-RUBY
83
+ ```
84
+ :::= irb --simple-prompt
85
+ a = 3
86
+ b = "foo" * a
87
+ puts b
88
+ ```
89
+ RUBY
90
+
91
+
92
+ Dir.mktmpdir do |dir|
93
+ Dir.chdir(dir) do
94
+
95
+ parsed = Rundoc::Parser.new(contents)
96
+ actual = parsed.to_md
97
+ expected = "```\n$ irb --simple-prompt\na = 3\n=> 3\r\nb = \"foo\" * a\n=> \"foofoofoo\"\r\nputs b\nfoofoofoo\r\n=> nil\n```\n"
98
+ assert_equal expected, actual
99
+ end
100
+ end
101
+
102
+ end
103
+
104
+ end
@@ -0,0 +1,200 @@
1
+ require 'test_helper'
2
+
3
+ class RegexTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_indent_regex
9
+
10
+ contents = <<-RUBY
11
+ foo
12
+
13
+ $ cd
14
+ yo
15
+ sup
16
+
17
+ bar
18
+ RUBY
19
+
20
+ regex = Rundoc::Parser::INDENT_BLOCK
21
+ parsed = contents.match(/#{regex}/).to_s
22
+ assert_equal "\n $ cd\n yo\n sup\n", parsed
23
+ end
24
+
25
+ def test_github_regex
26
+
27
+ contents = <<-RUBY
28
+ foo
29
+
30
+ ```
31
+ $ cd
32
+ yo
33
+ sup
34
+ ```
35
+
36
+ bar
37
+ RUBY
38
+
39
+ regex = Rundoc::Parser::GITHUB_BLOCK
40
+ parsed = contents.match(/#{regex}/m).to_s
41
+ assert_equal "```\n$ cd\nyo\nsup\n```\n", parsed
42
+ end
43
+
44
+ def test_github_tagged_regex
45
+ contents = <<-RUBY
46
+ foo
47
+
48
+ ```ruby
49
+ $ cd
50
+ yo
51
+ sup
52
+ ```
53
+
54
+ bar
55
+ RUBY
56
+
57
+ regex = Rundoc::Parser::GITHUB_BLOCK
58
+ parsed = contents.match(/#{regex}/m).to_s
59
+ assert_equal "```ruby\n$ cd\nyo\nsup\n```\n", parsed
60
+ end
61
+
62
+ def test_command_regex
63
+ regex = Rundoc::Parser::COMMAND_REGEX.call(":::")
64
+
65
+ contents = ":::$ mkdir schneems"
66
+ match = contents.match(regex)
67
+ assert_equal "", match[:tag]
68
+ assert_equal "$", match[:command]
69
+ assert_equal "mkdir schneems", match[:statement]
70
+
71
+ contents = ":::=$ mkdir schneems"
72
+ match = contents.match(regex)
73
+ assert_equal "=", match[:tag]
74
+ assert_equal "$", match[:command]
75
+ assert_equal "mkdir schneems", match[:statement]
76
+
77
+ contents = ":::= $ mkdir schneems"
78
+ match = contents.match(regex)
79
+ assert_equal "=", match[:tag]
80
+ assert_equal "$", match[:command]
81
+ assert_equal "mkdir schneems", match[:statement]
82
+
83
+ contents = ":::-$ mkdir schneems"
84
+ match = contents.match(regex)
85
+ assert_equal "-", match[:tag]
86
+ assert_equal "$", match[:command]
87
+ assert_equal "mkdir schneems", match[:statement]
88
+
89
+ contents = ":::- $ mkdir schneems"
90
+ match = contents.match(regex)
91
+ assert_equal "-", match[:tag]
92
+ assert_equal "$", match[:command]
93
+ assert_equal "mkdir schneems", match[:statement]
94
+ end
95
+
96
+
97
+ def test_codeblock_regex
98
+
99
+ contents = <<-RUBY
100
+ foo
101
+
102
+ ```
103
+ :::$ mkdir
104
+ ```
105
+
106
+ zoo
107
+
108
+ ```
109
+ :::$ cd ..
110
+ something
111
+ ```
112
+
113
+ bar
114
+ RUBY
115
+
116
+ regex = Rundoc::Parser::CODEBLOCK_REGEX
117
+
118
+ actual = contents.partition(regex)
119
+ expected = ["foo\n\n",
120
+ "```\n:::$ mkdir\n```\n",
121
+ "\nzoo\n\n```\n:::$ cd ..\nsomething\n```\n\nbar\n"]
122
+
123
+ assert_equal expected, actual
124
+
125
+ str = "```\n:::$ mkdir\n```\n"
126
+ match = str.match(regex)
127
+ assert_equal ":::$ mkdir\n", match[:contents]
128
+
129
+ str = "\n\n```\n:::$ cd ..\nsomething\n```\n\nbar\n"
130
+ match = str.match(regex)
131
+ assert_equal ":::$ cd ..\nsomething\n", match[:contents]
132
+
133
+ # partition, shift, codebloc,
134
+ end
135
+
136
+ def test_complex_regex
137
+
138
+ contents = <<-RUBY
139
+ ```java
140
+ :::= write app/controllers/Application.java
141
+ package controllers;
142
+
143
+ import static java.util.concurrent.TimeUnit.SECONDS;
144
+ import models.Pinger;
145
+ import play.libs.Akka;
146
+ import play.libs.F.Callback0;
147
+ import play.mvc.Controller;
148
+ import play.mvc.Result;
149
+ import play.mvc.WebSocket;
150
+ import scala.concurrent.duration.Duration;
151
+ import views.html.index;
152
+ import akka.actor.ActorRef;
153
+ import akka.actor.Cancellable;
154
+ import akka.actor.Props;
155
+
156
+ public class Application extends Controller {
157
+ public static WebSocket<String> pingWs() {
158
+ return new WebSocket<String>() {
159
+ public void onReady(WebSocket.In<String> in, WebSocket.Out<String> out) {
160
+ final ActorRef pingActor = Akka.system().actorOf(Props.create(Pinger.class, in, out));
161
+ final Cancellable cancellable = Akka.system().scheduler().schedule(Duration.create(1, SECONDS),
162
+ Duration.create(1, SECONDS),
163
+ pingActor,
164
+ "Tick",
165
+ Akka.system().dispatcher(),
166
+ null
167
+ );
168
+
169
+ in.onClose(new Callback0() {
170
+ @Override
171
+ public void invoke() throws Throwable {
172
+ cancellable.cancel();
173
+ }
174
+ });
175
+ }
176
+
177
+ };
178
+ }
179
+
180
+ public static Result pingJs() {
181
+ return ok(views.js.ping.render());
182
+ }
183
+
184
+ public static Result index() {
185
+ return ok(index.render());
186
+ }
187
+ }
188
+ ```
189
+ RUBY
190
+
191
+ regex = Rundoc::Parser::CODEBLOCK_REGEX
192
+ match = contents.match(regex)
193
+ assert_equal 'java', match[:lang]
194
+ assert_equal '```', match[:fence]
195
+ assert_equal '`', match[:fence_char]
196
+
197
+ assert_equal contents.strip, match.to_s.strip
198
+ end
199
+
200
+ end