narabi 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -2
- data/features/parser.feature +32 -4
- data/features/step_definitions/parser_steps.rb +26 -9
- data/lib/narabi/parser.rb +50 -11
- data/lib/narabi/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
data/features/parser.feature
CHANGED
@@ -14,10 +14,24 @@
|
|
14
14
|
|
15
15
|
例:
|
16
16
|
| 入力値 | 送信元 | 受信先 | 電文 |
|
17
|
-
| "Foo->Bar: Some message" | "Foo" | "Bar" | "
|
18
|
-
| "Bar-->Foo: Response" | "Bar" | "Foo" | "
|
19
|
-
| " A 1 -> B 1 : M1 " | " A 1 " | " B 1 " | "
|
17
|
+
| "Foo->Bar: Some message" | "Foo" | "Bar" | "Some message" |
|
18
|
+
| "Bar-->Foo: Response" | "Bar" | "Foo" | "Response" |
|
19
|
+
| " A 1 -> B 1 : M1 " | " A 1 " | " B 1 " | "M1 " |
|
20
20
|
| "太郎->花子:" | "太郎" | "花子" | "" |
|
21
|
+
| "太郎->花子: " | "太郎" | "花子" | " " |
|
22
|
+
|
23
|
+
シナリオアウトライン: ノートの解析に成功
|
24
|
+
前提 一行の文字列<入力値>を入力する
|
25
|
+
もし 一行の文字列を解析する
|
26
|
+
ならば 送信元は<送信元>となること
|
27
|
+
かつ 受信先は<受信先>となること
|
28
|
+
かつ 電文は<電文>となること
|
29
|
+
|
30
|
+
例:
|
31
|
+
| 入力値 | 送信元 | 受信先 | 電文 |
|
32
|
+
| "note User: This is a note." | "User" | "User" | "This is a note." |
|
33
|
+
| "note User:" | "User" | "User" | "" |
|
34
|
+
| "note F 1: http://..." | "F 1" | "F 1" | "http://..." |
|
21
35
|
|
22
36
|
シナリオアウトライン: 呼び出しと応答
|
23
37
|
前提 一行の文字列<入力値>を入力する
|
@@ -28,14 +42,28 @@
|
|
28
42
|
| 入力値 | 種類 |
|
29
43
|
| "Foo->Bar: Some message" | 呼び出し |
|
30
44
|
| "Bar-->Foo: Response" | 応答 |
|
45
|
+
| "note Foo: note" | ノート |
|
31
46
|
|
32
47
|
シナリオアウトライン: 不正な行を解析
|
33
48
|
前提 一行の文字列<入力値>を入力する
|
34
49
|
もし 一行の文字列を解析する
|
35
|
-
ならば
|
50
|
+
ならば メッセージは不正
|
36
51
|
|
37
52
|
例:
|
38
53
|
| 入力値 |
|
39
54
|
| "Foo->Bar" |
|
40
55
|
| "Foo-->Bar" |
|
41
56
|
| "" |
|
57
|
+
|
58
|
+
シナリオアウトライン: インスタンスの解析に成功
|
59
|
+
前提 一行の文字列<入力値>を入力する
|
60
|
+
もし 一行のインスタンス文字列を解析する
|
61
|
+
ならば インスタンスは<名称>となること
|
62
|
+
|
63
|
+
例:
|
64
|
+
| 入力値 | 名称 |
|
65
|
+
| "instance User" | "User" |
|
66
|
+
| "instance " | " " |
|
67
|
+
| "instance " | " " |
|
68
|
+
| "instance " | " " |
|
69
|
+
|
@@ -12,27 +12,44 @@ end
|
|
12
12
|
@output = Narabi.parse_line(@input)
|
13
13
|
end
|
14
14
|
|
15
|
-
ならば /^送信元は"(#{CAPTURE_STRING})"となること$/ do |
|
16
|
-
@output.
|
15
|
+
ならば /^送信元は"(#{CAPTURE_STRING})"となること$/ do |expected_from|
|
16
|
+
@output[:from].should == expected_from
|
17
17
|
end
|
18
18
|
|
19
|
-
ならば /^受信先は"(#{CAPTURE_STRING})"となること$/ do |
|
20
|
-
@output.
|
19
|
+
ならば /^受信先は"(#{CAPTURE_STRING})"となること$/ do |expected_to|
|
20
|
+
@output[:to].should == expected_to
|
21
21
|
end
|
22
22
|
|
23
|
-
ならば
|
24
|
-
@output.
|
23
|
+
ならば /^受信先はnilとなること$/ do
|
24
|
+
@output[:to].should be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
ならば /^電文は"(#{CAPTURE_STRING})"となること$/ do |expected_body|
|
28
|
+
@output[:body].should == expected_body
|
25
29
|
end
|
26
30
|
|
27
31
|
ならば /^種類は呼び出しとなること$/ do
|
28
|
-
@output.
|
32
|
+
@output[:is_return].should be_false
|
33
|
+
@output[:is_note].should be_false
|
29
34
|
end
|
30
35
|
|
31
36
|
ならば /^種類は応答となること$/ do
|
32
|
-
@output.
|
37
|
+
@output[:is_return].should be_true
|
38
|
+
@output[:is_note].should be_false
|
39
|
+
end
|
40
|
+
|
41
|
+
ならば /^種類はノートとなること$/ do
|
42
|
+
@output[:is_note].should be_true
|
33
43
|
end
|
34
44
|
|
35
|
-
ならば
|
45
|
+
ならば /^メッセージは不正$/ do
|
36
46
|
@output.should == nil
|
37
47
|
end
|
38
48
|
|
49
|
+
もし /^一行のインスタンス文字列を解析する$/ do
|
50
|
+
@output = Narabi::Instance.parse_line(@input)
|
51
|
+
end
|
52
|
+
|
53
|
+
ならば /^インスタンスは"(.*?)"となること$/ do |expected_name|
|
54
|
+
@output[:name].should == expected_name
|
55
|
+
end
|
data/lib/narabi/parser.rb
CHANGED
@@ -1,22 +1,61 @@
|
|
1
1
|
module Narabi
|
2
|
-
NORMAL_REGEXP = /^(
|
3
|
-
RESPONSE_REGEXP = /^(
|
2
|
+
NORMAL_REGEXP = /^(?<from>.+)->(?<to>.+):\s?(?<body>.*)/
|
3
|
+
RESPONSE_REGEXP = /^(?<from>.+)-->(?<to>.+):\s?(?<body>.*)/
|
4
|
+
NOTE_REGEXP = /^note\s(?<from>(\s|\d|\w|^:)+):\s?(?<body>.*)/
|
5
|
+
INSTANCE_REGEXP = /^instance\s?(?<name>.+)/
|
4
6
|
|
5
|
-
class
|
6
|
-
|
7
|
+
class Base
|
8
|
+
def self.try_to_create(regexp, src)
|
9
|
+
return nil unless match = regexp.match(src)
|
10
|
+
indexes = regexp.named_captures
|
11
|
+
hash = {}
|
12
|
+
regexp.names.each do |name|
|
13
|
+
hash[name.to_sym] = match[indexes[name].first]
|
14
|
+
end
|
15
|
+
hash
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Message
|
20
|
+
def self.create_normal(src)
|
21
|
+
msg = Base.try_to_create(NORMAL_REGEXP, src)
|
22
|
+
#msg[:is_return] = false if msg
|
23
|
+
#msg[:is_note] = false if msg
|
24
|
+
msg
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.create_return(src)
|
28
|
+
msg = Base.try_to_create(RESPONSE_REGEXP, src)
|
29
|
+
msg[:is_return] = true if msg
|
30
|
+
#msg[:is_note] = false if msg
|
31
|
+
msg
|
32
|
+
end
|
7
33
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
34
|
+
def self.create_note(src)
|
35
|
+
msg = Base.try_to_create(NOTE_REGEXP, src)
|
36
|
+
msg[:to] = msg[:from] if msg
|
37
|
+
#msg[:is_return] = false if msg
|
38
|
+
#msg[:to] = "" if msg
|
39
|
+
msg[:is_note] = true if msg
|
40
|
+
msg
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Instance
|
45
|
+
def self.parse_line(src)
|
46
|
+
msg = Base.try_to_create(INSTANCE_REGEXP, src)
|
11
47
|
end
|
12
48
|
end
|
13
49
|
|
14
50
|
def self.parse_line(src)
|
15
|
-
if
|
16
|
-
return
|
51
|
+
if msg = Message.create_return(src)
|
52
|
+
return msg
|
53
|
+
end
|
54
|
+
if msg = Message.create_normal(src)
|
55
|
+
return msg
|
17
56
|
end
|
18
|
-
if
|
19
|
-
return
|
57
|
+
if msg = Message.create_note(src)
|
58
|
+
return msg
|
20
59
|
end
|
21
60
|
end
|
22
61
|
end
|
data/lib/narabi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: narabi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05
|
12
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|