graffle 0.1.0 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/History.txt +5 -0
  2. data/LICENSE.txt +2 -2
  3. data/Manifest.txt +7 -15
  4. data/Rakefile +5 -10
  5. data/Rakefile.hoe +9 -4
  6. data/bin/bin-skeleton +1 -11
  7. data/graffle.tmproj +2 -310
  8. data/lib/graffle/stereotypes.rb +32 -19
  9. data/lib/graffle/styled-text-reader.rb +0 -5
  10. data/lib/graffle/version.rb +1 -1
  11. data/lib/grail_test.rb +16 -0
  12. data/lib/grail_test/command-interpreters.rb +78 -0
  13. data/test/grails-tests/commands-from-quoted-args-tests.rb +86 -0
  14. data/test/grails-tests/destinations-tests.rb +19 -0
  15. data/test/grails-tests/do-nothing-commands-tests.rb +18 -0
  16. data/test/grails-tests/graphic-interpreter-tests.rb +70 -0
  17. data/test/grails-tests/translation-testcase.rb +25 -0
  18. data/test/line-graphic-tests.rb +10 -0
  19. data/test/set-standalone-test-paths.rb +4 -1
  20. data/test/shaped-graphic-tests.rb +6 -2
  21. data/test/styled-text-reader-tests.rb +0 -12
  22. data/test/text-tests.rb +9 -5
  23. metadata +25 -20
  24. data/lib/graffle/third-party/s4t-utils.rb +0 -21
  25. data/lib/graffle/third-party/s4t-utils/capturing-globals.rb +0 -78
  26. data/lib/graffle/third-party/s4t-utils/claims.rb +0 -14
  27. data/lib/graffle/third-party/s4t-utils/command-line.rb +0 -15
  28. data/lib/graffle/third-party/s4t-utils/error-handling.rb +0 -20
  29. data/lib/graffle/third-party/s4t-utils/friendly-format.rb +0 -27
  30. data/lib/graffle/third-party/s4t-utils/hacks.rb +0 -32
  31. data/lib/graffle/third-party/s4t-utils/load-path-auto-adjuster.rb +0 -120
  32. data/lib/graffle/third-party/s4t-utils/more-assertions.rb +0 -29
  33. data/lib/graffle/third-party/s4t-utils/os.rb +0 -28
  34. data/lib/graffle/third-party/s4t-utils/rake-task-helpers.rb +0 -75
  35. data/lib/graffle/third-party/s4t-utils/rakefile-common.rb +0 -106
  36. data/lib/graffle/third-party/s4t-utils/svn-file-movement.rb +0 -101
  37. data/lib/graffle/third-party/s4t-utils/test-util.rb +0 -19
  38. data/lib/graffle/third-party/s4t-utils/version.rb +0 -3
@@ -223,8 +223,16 @@ module Graffle
223
223
 
224
224
 
225
225
 
226
- # The Text within the object (or nil if there isn't any).
227
- def content; self['Text']; end
226
+ # The Text within the object. If there is no text, the return
227
+ # value is a null object that responds to as_lines with an empty
228
+ # array.
229
+ def content
230
+ if has_key?('Text')
231
+ self['Text']
232
+ else
233
+ Text::Null.new
234
+ end
235
+ end
228
236
  alias_method :contents, :content
229
237
 
230
238
  def with_text(string) # :nodoc:
@@ -288,6 +296,17 @@ module Graffle
288
296
  Point.new(self['Points'][0])
289
297
  end
290
298
 
299
+ # A quick way to get the lines from a text's label (as plain text).
300
+ # If the line doesn't have a label, label_lines returns the empty array.
301
+ def label_lines
302
+ if label
303
+ label.content.as_lines
304
+ else
305
+ label || []
306
+ end
307
+ end
308
+
309
+
291
310
  private
292
311
  def points_at(*points)
293
312
  self['Points'] = points.collect { |p| "{#{p[0]}, #{p[1]}}" }
@@ -422,25 +441,19 @@ module Graffle
422
441
  # Strip all the styling from the RTF string and return it as
423
442
  # humble ASCII.
424
443
  def as_plain_text
425
- StyledTextReader.new(self.as_rtf).as_lines.join("\n")
444
+ as_lines.join("\n")
426
445
  end
427
446
 
428
- # Return an array of arrays. Each of the inner arrays represents
429
- # a line in the original. At the moment, the inner array is what
430
- # you get when you split the line at double-quote boundaries. For
431
- # example, given this string:
432
- #
433
- # He visits the "login" page.
434
- # His login is "peter", his password is "paul"
435
- #
436
- # the method returns:
437
- #
438
- # [ ['He visits the', 'login', 'page.']
439
- # ['His login is', 'peter', 'his password is', 'paul']]
440
- #
441
- # There's more to come here.
442
- def as_tokens_within_lines
443
- StyledTextReader.new(self.as_rtf).as_tokens_within_lines
447
+ # Return an array of lines, each line as_plain_text.
448
+ def as_lines
449
+ StyledTextReader.new(self.as_rtf).as_lines
450
+ end
451
+
452
+ class Null # Null object for text
453
+ def as_lines; []; end
444
454
  end
455
+
445
456
  end
457
+
458
+
446
459
  end
@@ -41,11 +41,6 @@ module Graffle
41
41
  end
42
42
  end
43
43
 
44
- def as_tokens_within_lines
45
- self.as_lines.collect do |line|
46
- line.split(/",?/).collect { |tok| tok.strip }
47
- end
48
- end
49
44
  end
50
45
 
51
46
 
@@ -4,5 +4,5 @@
4
4
  # Copyright (c) 2007. All rights reserved.
5
5
 
6
6
  module Graffle
7
- Version = '0.1.0'
7
+ Version = '0.1.7'
8
8
  end
data/lib/grail_test.rb ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Brian Marick on 2007-07-10.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+
7
+
8
+ require 'graffle'
9
+ require 'grail_test/command-interpreters'
10
+
11
+ # See README.txt[link:files/README_txt.html]
12
+ module GrailTest
13
+
14
+ end
15
+
16
+ GrailTests=GrailTest # Be tolerant of typos
@@ -0,0 +1,78 @@
1
+ require 'enumerator'
2
+
3
+ module GrailTest
4
+
5
+ class GraphicInterpreter
6
+
7
+ def initialize(graphics, hash = {})
8
+ @graphics = graphics
9
+ @line_label_strategy =
10
+ hash['interpret line labels as'] || DoNothingCommands
11
+ @shaped_graphic_text_strategy =
12
+ hash['interpret shaped graphic text as'] || DoNothingCommands
13
+ end
14
+
15
+ def run_against(target)
16
+ @target = target
17
+ @graphics.each do |g|
18
+ case g.stereotype.basename
19
+ when "ShapedGraphic"
20
+ process_commands(@shaped_graphic_text_strategy.new,
21
+ g.content.as_lines)
22
+ when "LineGraphic"
23
+ process_commands(@line_label_strategy.new,
24
+ g.label_lines)
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def process_commands(command_maker, command_lines)
32
+ command_lines.each do |line|
33
+ info = command_maker.produce_method_call(line)
34
+ @target.send(*info)
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+ class CommandsWithQuotedArgs
41
+
42
+ def produce_method_call(string)
43
+ quote_marker = '%<<<==-'
44
+
45
+ string = string.gsub(/(\w)'(\w)/, "#{quote_marker}\\1\\2#{quote_marker}")
46
+ tokens = string.split(/['"],?/, allow_trailing_null_fields = -1)
47
+ message = tokens[0].strip
48
+ args = tokens[1..-1]
49
+
50
+
51
+ message.gsub!(/#{quote_marker}(.)(.)#{quote_marker}/, "\\1\\2")
52
+ message.gsub!(/\s+/,'_')
53
+ message.gsub!(/-/, '_')
54
+ message.gsub!(/\W/, '')
55
+
56
+
57
+ args = args.enum_slice(2).collect { |e| e[0] }
58
+ args = args.collect do | arg |
59
+ arg.gsub(/#{quote_marker}(.)(.)#{quote_marker}/, "\\1'\\2")
60
+ end
61
+
62
+ [message] + args
63
+ end
64
+ end
65
+
66
+ class Destinations
67
+ def produce_method_call(string)
68
+ ['assert_on_page', string.downcase]
69
+ end
70
+ end
71
+
72
+ class DoNothingCommands
73
+ def produce_method_call(string)
74
+ [:object_id] # no_op
75
+ end
76
+ end
77
+ end
78
+
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Brian Marick on 2007-07-06.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ require "../set-standalone-test-paths.rb" unless $started_from_rakefile
7
+ require 'test/grails-tests/translation-testcase'
8
+
9
+ class TestCommandsFromQuotedArgs < TranslationTestCase
10
+
11
+
12
+ # TODO: put this somewhere right.
13
+ PLAIN = ['John follows "sign up" link',
14
+ 'he logs in with "john", password "fred", and auth key "foo".',
15
+ 'plain command with trailing period',
16
+ 'Plain command - (upper case and special chars)'
17
+ ]
18
+
19
+ # TODO: put this somewhere right.
20
+ RTF = %Q{
21
+ | {\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf420
22
+ | {\\fonttbl\\f0\\fswiss\\fcharset77 Helvetica;}
23
+ | {\\colortbl;\\red255\\green255\\blue255;}
24
+ | \\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\qc\\pardirnatural
25
+ |
26
+ | \\f0\\fs24 \\cf0 #{PLAIN[0]}\\
27
+ | #{PLAIN[1]}\\
28
+ | #{PLAIN[2]}\\
29
+ | #{PLAIN[3]}}}.trim('|')
30
+
31
+
32
+ def setup
33
+ @interpreter = CommandsWithQuotedArgs.new
34
+ end
35
+
36
+ def test_translation
37
+ # Underscoring
38
+ assert_translation(['token'], "token")
39
+ assert_translation(['underscore_separated_tokens'],
40
+ 'underscore separated tokens'
41
+ )
42
+ assert_translation(['whitespace_is_handled_intelligently'],
43
+ " whitespace\tis handled intelligently "
44
+ )
45
+ assert_translation(['here_is_an', 'argument'],
46
+ 'here is an "argument"'
47
+ )
48
+ assert_translation(['single_spaces_are_args', 'too '],
49
+ " single spaces are args 'too ' "
50
+ )
51
+ assert_translation(['multiple', 'arguments', "work"],
52
+ %q{multiple 'arguments' "work"}
53
+ )
54
+ assert_translation(['only_the', 'prefix', "command"],
55
+ %q{only the 'prefix' is included in the 'command'}
56
+ )
57
+
58
+ assert_translation(['arguments_can', 'be', 'comma', "separated"],
59
+ %q{arguments can 'be', 'comma', and 'separated'}
60
+ )
61
+
62
+ assert_translation(['stuff_after_the', 'last', 'argument'],
63
+ %q{stuff after the 'last' 'argument' is ignored.}
64
+ )
65
+
66
+ assert_translation(['even_if_it_is_just', 'a period'],
67
+ %q{even if it is just 'a period'.}
68
+ )
69
+
70
+ assert_translation(['the_method_ignores_punctuation'],
71
+ %q{the method (ignores) punctuation.}
72
+ )
73
+
74
+ assert_translation(['except_that_dashes_become_underscore'],
75
+ %q{except that dashes become-underscore}
76
+ )
77
+
78
+ assert_translation(["its_possible_to_have_quotes_within_words"],
79
+ %q{it's possible to have quotes within words}
80
+ )
81
+
82
+ assert_translation(["while_a_messages_quotes_are_stripped", "an argument's are retained"],
83
+ %q{while a message's quotes are stripped, "an argument's are retained"}
84
+ )
85
+ end
86
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Brian Marick on 2007-07-06.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ require "../set-standalone-test-paths.rb" unless $started_from_rakefile
7
+ require 'test/grails-tests/translation-testcase'
8
+
9
+ class TestDestinationCheckCreation < TranslationTestCase
10
+
11
+ def setup
12
+ @interpreter = Destinations.new
13
+ end
14
+
15
+ def test_translation
16
+ assert_translation(['assert_on_page', "markup"],
17
+ "MARKUP")
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Brian Marick on 2007-07-06.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ require "../set-standalone-test-paths.rb" unless $started_from_rakefile
7
+ require 'test/grails-tests/translation-testcase'
8
+
9
+ class TestCommandsThatDoNothing < TranslationTestCase
10
+
11
+ def setup
12
+ @interpreter = DoNothingCommands.new
13
+ end
14
+
15
+ def test_translation
16
+ assert_translation([:object_id], "MARKUP")
17
+ end
18
+ end
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Brian Marick on 2007-07-10.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ require 'test/unit'
7
+ require 's4t-utils'
8
+ include S4tUtils
9
+
10
+ require "../set-standalone-test-paths.rb" unless $started_from_rakefile
11
+ require 'test/grails-tests/translation-testcase'
12
+
13
+ require 'extensions/string'
14
+ require 'grail_test'
15
+
16
+
17
+ class GraphicInterpreterTest < Test::Unit::TestCase
18
+ include Graffle::Builders
19
+ include GrailTest
20
+
21
+ class CommandRecorder
22
+ attr_reader :record
23
+ def initialize
24
+ @record = []
25
+ end
26
+
27
+ def method_missing(message, *args)
28
+ @record << "#{message}(" + args.collect { | a | a.inspect }.join(", ") + ')'
29
+ end
30
+ end
31
+
32
+ def two_graphics
33
+ s = sheet {
34
+ with line_label {
35
+ for_line 1
36
+ with_text 'produce "argument"'
37
+ }
38
+ with line_graphic {
39
+ graffle_id_is 1
40
+ }
41
+ with shaped_graphic {
42
+ graffle_id_is 2
43
+ with_text "SIGNUP"
44
+ }
45
+ }
46
+
47
+ [s.find_by_id(1), s.find_by_id(2)]
48
+ end
49
+
50
+ def assert_commands_produced(expected, interpreter)
51
+ recorder = CommandRecorder.new
52
+ interpreter.run_against(recorder)
53
+ assert_equal(expected, recorder.record)
54
+ end
55
+
56
+
57
+ def test_interpreter_applies_strategies
58
+ interpreter = GraphicInterpreter.new(two_graphics,
59
+ 'interpret line labels as' => CommandsWithQuotedArgs,
60
+ 'interpret shaped graphic text as' => Destinations)
61
+ assert_commands_produced(['produce("argument")', 'assert_on_page("signup")'],
62
+ interpreter)
63
+ end
64
+
65
+
66
+ def test_interpreter_can_omit_strategies
67
+ interpreter = GraphicInterpreter.new(two_graphics)
68
+ assert_commands_produced([], interpreter)
69
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Brian Marick on 2007-07-10.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+
7
+ require 'test/unit'
8
+ require 's4t-utils'
9
+ include S4tUtils
10
+
11
+ require 'extensions/string'
12
+ require 'grail_test'
13
+
14
+
15
+ class TranslationTestCase < Test::Unit::TestCase
16
+ include GrailTests
17
+
18
+ def test_default; "quiet, test/unit!"; end
19
+
20
+ def assert_translation(expected, original)
21
+ actual = @interpreter.produce_method_call(original)
22
+ assert_equal(expected, actual)
23
+ end
24
+
25
+ end
@@ -151,5 +151,15 @@ class TestLineGraphics < Test::Unit::TestCase
151
151
  assert_equal("the line has a label", line.label_rtf.as_plain_text)
152
152
  end
153
153
 
154
+ def test_a_quick_way_to_get_to_label_text_as_lines
155
+ line = line_label_sheet.find_by_id(1)
156
+ assert_equal(["the line has a label"], line.label_lines)
157
+ end
158
+
159
+ def test_missing_labels_can_be_asked_for_their_nonexistent_lines
160
+ line = sheet { with line_graphic }.graphics.first
161
+ assert_equal([], line.label_lines)
162
+ end
163
+
154
164
 
155
165
  end
@@ -1,5 +1,8 @@
1
1
  require 'pathname'
2
+ require 's4t-utils'
3
+
2
4
 
3
5
  PACKAGE_ROOT = Pathname.new(__FILE__).parent.parent.to_s
4
6
  $:.unshift("#{PACKAGE_ROOT}/lib")
5
- require 'graffle/third-party/s4t-utils/load-path-auto-adjuster'
7
+ $:.unshift(PACKAGE_ROOT)
8
+