Skip to content

Commit 13e75c8

Browse files
committed
Add specs
1 parent ad20c1f commit 13e75c8

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format documentation

lib/oreilly/snippets.rb

+35-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@
22

33
module Oreilly
44
module Snippets
5-
# Your code goes here...
5+
def self.process( input, filename, language=nil, identifier=nil )
6+
input
7+
end
8+
9+
def self.parse( input, offset=nil )
10+
output = []
11+
input.scan( /\[([^=]*)="([^"]*)",\s*([^=]*)="([^"]*)",\s*([^=]*)="([^"]*)"\]\n(.*)\n(.*)\n\7/mx ) do |m|
12+
match = {}
13+
3.times do |i|
14+
match[m[i*2].to_sym] = m[(i*2)+1]
15+
end
16+
17+
match[:snippet] = m[6]
18+
match[:throwaway] = m[7]
19+
20+
output << match
21+
end
22+
output
23+
end
624
end
725
end
26+
# To include this snippet in an AsciiDoc file, add the following block:
27+
28+
# [filename="factorial.js", language="js", identifier="FACTORIAL_FUNC"]
29+
# snippet~~~~
30+
# Put any descriptive text you want here. It will be replaced with the
31+
# specified code snippet when you build ebook outputs
32+
# snippet~~~~
33+
# When the ebook is generated, the following output will be present in place of the snippet block:
34+
35+
# function factorial(number) {
36+
# if (number == 0) {
37+
# return 1
38+
# } else {
39+
# return factorial(number - 1) * number
40+
# }
41+
# }

oreilly-snippets.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
2020

2121
spec.add_development_dependency "bundler", "~> 1.3"
2222
spec.add_development_dependency "rake"
23+
spec.add_development_dependency "rspec"
2324
end

spec/process_spec.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'spec_helper'
2+
3+
TEMPLATE = <<END
4+
5+
ABC
6+
7+
[filename="fixtures/factorial.js", language="js", identifier="FACTORIAL_FUNC"]
8+
snippet~~~~
9+
Put any descriptive text you want here. It will be replaced with the
10+
specified code snippet when you build ebook outputs
11+
snippet~~~~
12+
13+
DEF
14+
15+
END
16+
17+
describe "#parse" do
18+
it "should parse the file and extract the correct things" do
19+
outputs = Oreilly::Snippets.parse( TEMPLATE )
20+
output = outputs[0]
21+
puts output.inspect
22+
output[:filename].should == "fixtures/factorial.js"
23+
output[:language].should == "js"
24+
output[:identifier].should == "FACTORIAL_FUNC"
25+
output[:throwaway].should match( /^Put any descriptive/ )
26+
output[:snippet].should == "snippet~~~~"
27+
end
28+
end
29+
30+
describe "#process" do
31+
32+
it "should process a simple file" do
33+
output = Oreilly::Snippets.process( TEMPLATE, "fixtures/", "asdas" )
34+
output.should match( /ABC/ )
35+
output.should match( /DEF/ )
36+
end
37+
38+
end

spec/spec_helper.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# Require this file using `require "spec_helper"` to ensure that it is only
4+
# loaded once.
5+
#
6+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7+
8+
require 'oreilly/snippets'
9+
10+
RSpec.configure do |config|
11+
config.treat_symbols_as_metadata_keys_with_true_values = true
12+
config.run_all_when_everything_filtered = true
13+
config.filter_run :focus
14+
15+
# Run specs in random order to surface order dependencies. If you find an
16+
# order dependency and want to debug it, you can fix the order by providing
17+
# the seed, which is printed after each run.
18+
# --seed 1234
19+
config.order = 'random'
20+
end

0 commit comments

Comments
 (0)