perception 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,155 @@
1
+
2
+ require 'perception' if $0 == __FILE__
3
+ require 'kyanite/array_of_enumerables'
4
+
5
+
6
+ module Perception #:nodoc
7
+ module StringSpreadI #:nodoc
8
+
9
+
10
+ def analyze_columns(options={})
11
+
12
+ # vorbereiten
13
+ stretch = options[:stretch] || 1 # soviel wird zu jedem Tabstop hinzuaddiert
14
+
15
+ # mehrzeilige Darstellung
16
+ if self.include?("\n")
17
+ result = ArrayOfEnumerables.new
18
+ self.split("\n").each do |zeile|
19
+ # print zeile.inspect_pp
20
+ # print zeile.analyze_columns(options).inspect_pp
21
+ # print "\n"
22
+ result << zeile.analyze_columns(options)
23
+ end
24
+ result.compact!
25
+ result = result.rectangle
26
+ result.transpose!
27
+ #pp result
28
+ result2 = result.collect { |spalte| (spalte.sort[-1]) + stretch}
29
+ #pp result2
30
+ return result2
31
+
32
+
33
+
34
+ # einzeilig -- TODO: Fehler beheben
35
+ else
36
+
37
+ # weiter vorbereiten
38
+ level_start = options[:level_start] || 1
39
+ level_end = options[:level_end] || 1
40
+ search = options[:search] || ','
41
+
42
+ # funktioniert nicht 100%ig. Fehler bei einem mehrzeiligen Hash, dessen Values Gleichheitszeichen enthalten.
43
+ # Hier verhindert wahrscheinlich die erste Zeile die Maskierung des Gleichheitszeichens -- sie enthält die {-Klammer.
44
+ result = self.mask( :level_start => level_start,
45
+ :level_end => level_end
46
+ ) {|s| s.tr(search, Perception::PPP_SEARCH)}
47
+ #pp result.split(Perception::PPP_SEARCH)
48
+
49
+ result = result.split(Perception::PPP_SEARCH).collect {|e| e.strip.size + stretch}
50
+ return result if result.size > 1
51
+ return nil
52
+
53
+ end # if Rekursion
54
+ end # def
55
+
56
+
57
+
58
+ # Erzwingt ein bestimmtes Zeichen nur an Tabstop-Positionen
59
+ # Mit position_add= 0 werden die Spaces vor dem gefundenen Zeichen eingefügt.
60
+ # Standard ist position_add= search.size, damit werden die Spaces hinter den gefundenen Zeichen eingefügt.
61
+ # Mit PPP_ANTI_NEWLINE werden Zeilen markiert, bei denen anschließend noch das \n entfernt werden kann.
62
+ def spread_line(tabstops=[], search=/,/, position_add=nil, start=0)
63
+ #puts "spread_line, self=#{self.inspect_pp} tabstops=#{tabstops.join('-')}"
64
+ position_add = search.inspect.size-2 unless position_add
65
+ leading_spaces = self.size - self.lstrip.size
66
+
67
+
68
+ # das auszurichtende Zeichen finden
69
+ pos_found = self.index(search, start)
70
+ return self if pos_found.nil?
71
+ anti_newline = (self[pos_found+position_add..-1].strip == '' ? PPP_ANTI_NEWLINE : '')
72
+
73
+
74
+ # Tabstops bestimmen: Modus feste Tabstops
75
+ if tabstops.size > 1
76
+ pos_shift = 0
77
+ tabstops.each do |ts|
78
+ if ts >= pos_found
79
+ pos_shift = ts + leading_spaces
80
+ break
81
+ end
82
+ end # do
83
+ end # modus
84
+
85
+ # Tabstops bestimmen: Modus "Tabstop alle n Zeichen" bzw. Fallback, wenn feste Tabstops nicht ausreichend waren
86
+ if (tabstops.size == 1 || pos_shift == 0 )
87
+ pos_shift = ((pos_found/tabstops[0]) + 1) * tabstops[0] + leading_spaces
88
+ end # if modus
89
+
90
+ # Tabstops einfügen
91
+ if pos_found < pos_shift
92
+ self.insert(pos_found+position_add, (' '*(pos_shift - pos_found - 1)) + anti_newline )
93
+ end
94
+ spread_line(tabstops, search, position_add, pos_shift + 1)
95
+ end # def
96
+
97
+
98
+
99
+ def spread(options={})
100
+ # vorbereiten
101
+ level_start = options[:level_start] || 1
102
+ level_end = options[:level_end] || 1
103
+ search = options[:search] || '='
104
+ tabstops = options[:tabstops]
105
+ position_add = options[:position_add] # Mit position_add= 0 werden die Spaces vor dem gefundenen Zeichen eingefügt.
106
+ # Standard ist position_add= search.size, damit werden die Spaces hinter den gefundenen Zeichen eingefügt.
107
+ return self if tabstops.empty?
108
+
109
+ # Spalte ausrichten
110
+ self.mask( :level_start => level_start,
111
+ :level_end => level_end
112
+ # :with_brackets => true
113
+ ) { |s|
114
+ s_neu = s.split("\n").collect do | zeile |
115
+ zeile.spread_line(tabstops, search, position_add)
116
+ end.join("\n")
117
+ replaces = [[/\x1F\n */ ,''],[/\x1F */ ,' ']] # PPP_ANTI_NEWLINE = 31.chr
118
+ s_neu.mgsub(replaces)
119
+ } # mask
120
+ end # def
121
+
122
+
123
+
124
+ end # module
125
+ end # module
126
+
127
+ class String #:nodoc:
128
+ include Perception::StringSpreadI
129
+ end
130
+
131
+
132
+
133
+
134
+
135
+ # -----------------------------------------------------------------------------------------
136
+ # ausprobieren
137
+ #
138
+ if $0 == __FILE__ then
139
+
140
+ # require File.join(File.dirname(__FILE__), '..', '..', 'demo', 'demo_pp' )
141
+ # Perception::DemoSee.see_all_demos
142
+
143
+ require 'rbconfig'
144
+ require 'facets/to_hash'
145
+
146
+
147
+ include Config
148
+ see CONFIG.to_a[0..-1].to_h
149
+ #see CONFIG.to_a[1..5].to_h
150
+
151
+ end # if
152
+
153
+
154
+
155
+
@@ -0,0 +1,114 @@
1
+
2
+ require 'test/unit/util/backtracefilter'
3
+ require 'kyanite/general/kernel'
4
+
5
+ module Perception
6
+ module BacktraceFilter
7
+
8
+ CALLER_RE = /(.*):([0-9]+)(:in \`(.*)')?/
9
+ def parse_caller( input )
10
+ input.collect do |c|
11
+ captures = CALLER_RE.match(c)
12
+ [captures[1], captures[2], captures[4]]
13
+ end
14
+ end
15
+
16
+ def parse_test_name( input )
17
+ test_full_name = input.split('(')
18
+ test_class = test_full_name[1][0..-2]
19
+ test_method = test_full_name[0]
20
+ [test_class, test_method]
21
+ end
22
+
23
+
24
+ def filter_backtrace_perception(backtrace, header)
25
+ result = filter_backtrace(backtrace, nil)
26
+ result = result.collect { |zeile| zeile.gsub(RUBYDIR,'RUBYDIR').gsub($0, "### #{$0}" ) }
27
+ result = parse_caller(result).collect { |file, line, method|
28
+ [ File.dirname(file),
29
+ File.basename(file),
30
+ line,
31
+ method ] unless method =~ /^assert_/
32
+ }
33
+ result.compact!
34
+ result.unshift header if header
35
+ # result.unshift %w{DIR FILE METHOD LINE} unless result.size == 1
36
+ result = result.inspect_see.
37
+ mask( :with_brackets => true,
38
+ :level_start => 0,
39
+ :level_end => 1,
40
+ :pattern => /\[/
41
+ ) {|s| s.tr('[]',' ')}
42
+
43
+ result = result.
44
+ mask( :with_brackets => true,
45
+ :level_start => 0,
46
+ :level_end => 1,
47
+ :pattern => /\'/
48
+ ) {|s| s.tr("',",' ')}
49
+
50
+ result
51
+ end
52
+
53
+ end
54
+ end
55
+
56
+
57
+ module Test
58
+ module Unit
59
+
60
+
61
+ class Error
62
+ include Perception::BacktraceFilter
63
+ def long_display
64
+ test_class, test_method = parse_test_name(@test_name)
65
+ header = ['', test_class, '', '']
66
+ backtrace = filter_backtrace_perception(@exception.backtrace, header)
67
+ "Error: #{message.tr("`","'")}\n#{backtrace}\n\n\n\n"
68
+ end
69
+ end # class
70
+
71
+
72
+ class Failure
73
+ include Perception::BacktraceFilter
74
+ include Util::BacktraceFilter
75
+ def long_display
76
+ test_class, test_method = parse_test_name(@test_name)
77
+ header = ['', test_class, '', '']
78
+ backtrace = filter_backtrace_perception(location, header)
79
+ "Failure: #{message.tr("`","'")}\n#{backtrace}\n\n\n\n"
80
+ end
81
+ end # class
82
+
83
+
84
+ end
85
+ end
86
+
87
+
88
+
89
+
90
+
91
+ module Perception
92
+ module Assertions
93
+
94
+ def assert_equal(expected, actual, message=nil)
95
+ if expected.kind_of?(Float) && actual.kind_of?(Float)
96
+ full_message = build_message(message, "<?> with some delta expected but was\n#{' '*15}<?>\n", expected, actual)
97
+ delta = 0.0000000000001
98
+ assert_block(full_message) { (expected - actual).abs <= delta }
99
+ else
100
+ full_message = build_message(message, "<?> expected but was\n#{' '*15}<?>\n", expected, actual)
101
+ assert_block(full_message) { expected == actual }
102
+ end
103
+ end
104
+
105
+ end
106
+ end
107
+
108
+ module Test
109
+ module Unit
110
+ class TestCase
111
+ include Perception::Assertions
112
+ end
113
+ end
114
+ end
data/lib/perception.rb ADDED
@@ -0,0 +1,36 @@
1
+
2
+ if $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+ # puts 'Path schon aktuell'
4
+ else
5
+ $:.unshift(File.dirname(__FILE__))
6
+ end
7
+
8
+
9
+ # Die Versionsnummer steht ausserdem
10
+ # * im Script '2 install gem.bat'
11
+ # * in der gem-Verkn�pfung
12
+ module Perception
13
+ VERSION = '0.1.5'
14
+ end
15
+
16
+
17
+
18
+ $KCODE = 'UTF8'
19
+ require 'pp'
20
+ require 'set'
21
+ require 'kyanite/string/mgsub'
22
+ require 'kyanite/string/nested'
23
+ require 'kyanite/general/callerutils'
24
+ require 'kyanite/array/array'
25
+
26
+
27
+ require 'perception/const'
28
+ require 'perception/perception_private'
29
+ require 'perception/perception_main'
30
+ require 'perception/ppp'
31
+ require 'perception/test'
32
+ require 'perception/string_spread'
33
+ require 'perception/numeric'
34
+ require 'perception/date_and_time'
35
+ require 'perception/logging'
36
+
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Führt alle Tests aus
4
+ #
5
+
6
+
7
+ # Test-Verzeichnis der Applikation
8
+ test_verzeichnis = File.expand_path(File.dirname(__FILE__) )
9
+
10
+ Dir["#{test_verzeichnis}/test_*.rb"].sort.each { |t| require t }
@@ -0,0 +1,54 @@
1
+
2
+ require 'kyanite/unit_test'
3
+ require 'perception'
4
+
5
+ class TestPerceptionDateAndTime < UnitTest
6
+
7
+ def setup
8
+ seee.init
9
+ seee.out = []
10
+ end
11
+
12
+
13
+ def test_010_time_inspect
14
+ test = Time.at(1296702800)
15
+ assert_equal '2011-02-03 Thursday 04:13:20', test.inspect_pp
16
+ assert_equal '2011-02-03 Thursday 04:13:20', test.inspect_see
17
+ assert_equal '2011-02-03 04:13', test.inspect_see(:precision => 1)
18
+ assert_equal '03.Feb.2011', test.inspect_see(:norm => :de, :precision => 0)
19
+ end
20
+
21
+
22
+ def test_020_time_see
23
+ test = Time.at(1296702800)
24
+ assert_equal "\n2011-02-03 Thursday 04:13:20", see_print(test)
25
+ setup
26
+ assert_equal "\n2011-02-03 Thursday 04:13:20", see_puts(test)
27
+ setup
28
+ assert_equal "\n2011-02-03 Thursday 04:13:20", see_pp(test)
29
+ setup
30
+ assert_equal "\n2011-02-03 Thursday 04:13:20", see(test)
31
+ setup
32
+ end
33
+
34
+
35
+
36
+
37
+ end # class
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
@@ -0,0 +1,104 @@
1
+
2
+ require 'kyanite/unit_test'
3
+ require 'perception'
4
+
5
+
6
+ class TestPerceptionNumeric < UnitTest
7
+
8
+ def setup
9
+ seee.init
10
+ seee.out = []
11
+ end
12
+
13
+
14
+ def test_010_significant
15
+ test = [12567.89, 1256.789, 123.56789, 100.01, 100.0, 99.9, 12.0, 12, 12.56789, 1.256789, 1.5, 0, 0.1256789, 0.01256789, 0.001256789, 0.0001256789 ]
16
+ expect = [12600, 1260, 124, 100, 100, 99.9, 12, 12, 12.6, 1.26, 1.5, 0, 0.126, 0.0126, 0.00126, 0.000126]
17
+ result = test.collect { |t| t.significant }
18
+ assert_equal expect, result
19
+ end
20
+
21
+
22
+ def test_020_float_inspect
23
+ test = [7213541, 553337, 12567.89, 1256.789, 123.56789, 100.01, 100.0, 99.9, 12.0, 12, 12.56789, 1.256789, 1.5, 0, 0.1256789, 0.01256789, 0.001256789, 0.0001256789, ]
24
+ expect = ['7 210 000 ',
25
+ ' 553 000 ',
26
+ ' 12 600 ',
27
+ ' 1260 ',
28
+ ' 124 ',
29
+ ' 100 ',
30
+ ' 100 ',
31
+ ' 99,9 ',
32
+ ' 12 ',
33
+ ' 12 ',
34
+ ' 12,6 ',
35
+ ' 1,26 ',
36
+ ' 1,5 ',
37
+ ' 0 ',
38
+ ' 0,126 ',
39
+ ' 0,0126',
40
+ ' 0,00126',
41
+ ' 0,000126']
42
+ result= test.collect { |t| t.to_f.inspect_see }
43
+ assert_equal expect, result
44
+
45
+ result= test.collect { |t| see(t.to_f).gsub("\n",'') }
46
+ assert_equal expect, result
47
+ end
48
+
49
+
50
+ def test_030_integer_inspect
51
+ assert_equal ' 0', 0.inspect_see
52
+ assert_equal ' -1000', -1000.inspect_see
53
+ assert_equal '-87 200 000', -87213541.inspect_see
54
+ assert_equal '988 000 000 000 000 128', 987654321987654321.inspect_see # Das ist falsch. Rubys Umwandlung to_i funktioniert hier nicht.
55
+ assert_equal '9 990 000 000 000 000 000', 9987654321987654321.inspect_see # Das ist wieder richtig.
56
+ end
57
+
58
+
59
+ def test_040_see
60
+ test = 0
61
+ result = ' 0'
62
+ assert_equal "\n" + result, see(test); setup
63
+ assert_equal "\n" + result, see_pp(test); setup
64
+ assert_equal "\n" + result, see_puts(test); setup
65
+ assert_equal "\n" + result, see_print(test); setup
66
+ assert_equal "\n" + result, see_print(test); setup
67
+
68
+ test = 9987654321987654321
69
+ result = '9 990 000 000 000 000 000'
70
+ assert_equal "\n" + result, see(test); setup
71
+ assert_equal "\n" + result, see_pp(test); setup
72
+ assert_equal "\n" + result, see_puts(test); setup
73
+ assert_equal "\n" + result, see_print(test); setup
74
+ assert_equal "\n" + result, see_print(test); setup
75
+
76
+ test = -0.000314162353463463136537846595
77
+ result = ' -0,000314'
78
+ assert_equal "\n" + result, see(test); setup
79
+ assert_equal "\n" + result, see_pp(test); setup
80
+ assert_equal "\n" + result, see_puts(test); setup
81
+ assert_equal "\n" + result, see_print(test); setup
82
+ assert_equal "\n" + result, see_print(test); setup
83
+ end
84
+
85
+
86
+
87
+ end # class
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+