gir_ffi-pretty_printer 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c0972787e8016633eadd896926b50eb5cb337e9
4
- data.tar.gz: d4799c0d388521fce609befae9993b2f74588327
3
+ metadata.gz: b2a81f357003e2ff4209471ee6b1cc08a460919e
4
+ data.tar.gz: c7e37c2dd1a1c521247b0177098dfbfd90fd73cf
5
5
  SHA512:
6
- metadata.gz: 86b9372afa74baceeeb287269d6e38653652778da8e6bf87de1e5e4b313f96f875250f18622954cd24b10c559dd08baf5204017fb8dce0e9b63c0afd68a63ef2
7
- data.tar.gz: f134b17cc17e7b188feabd2195aeb4d27b0ff1dc61b8d3da75a390db86eb61a15c7ff7909ed929c4a5e5b8f5d4c5028a30a574e7d912f0d858d5a17d64bb5ae5
6
+ metadata.gz: db586351335e44d0424323ba4cb8884679cf2009017a90c9d742275a15fd0c72be2daf791a75f956cfe3b8e54817fb8d9c8737cab3f4e049ad241f054668cd2a
7
+ data.tar.gz: 7c80943037288f0dc9f819eabe84d28d9f2269a319275446c4fe3120a4529b5ccab9a0bb370d46eeaacf6add153ff55e81dd2ee91057440e98d8e524fe8b984f
data/README.md CHANGED
@@ -6,18 +6,17 @@ Pretty-printer for modules generated by GirFFI.
6
6
 
7
7
  * Prints generated, overridden and hand-added methods.
8
8
  * Uses the `mvz-live_ast` gem to inspect runtime-generated methods.
9
- * Needs MRI 1.9.3 or higher.
9
+ * Needs MRI 2.1 or higher.
10
10
 
11
11
  ## Usage
12
12
 
13
- # The pretty-printer must be required first!
14
13
  require 'gir_ffi-pretty_printer'
15
- require 'gir_ffi'
16
14
 
17
15
  # Any version
18
- GirFFI::PrettyPrinter.new.pretty_print 'GObject'
16
+ puts GirFFI::PrettyPrinter.new.pretty_print 'GObject'
17
+
19
18
  # A specific version
20
- GirFFI::PrettyPrinter.new.pretty_print 'Gtk', '3.0'
19
+ puts GirFFI::PrettyPrinter.new.pretty_print 'Gtk', '3.0'
21
20
 
22
21
  ## Install
23
22
 
@@ -25,7 +24,7 @@ Pretty-printer for modules generated by GirFFI.
25
24
 
26
25
  ## License
27
26
 
28
- Copyright © 2012--2014, [Matijs van Zuijlen](http://www.matijs.net/)
27
+ Copyright © 2012–2015, [Matijs van Zuijlen](http://www.matijs.net/)
29
28
 
30
29
  GirFFI-PrettyPrinter is free software, distributed under the terms of
31
30
  the GNU Lesser General Public License, version 2.1 or later. See the
data/Rakefile CHANGED
@@ -1,27 +1,28 @@
1
+ require 'rake/clean'
2
+ require 'bundler/gem_tasks'
1
3
  require 'rake/testtask'
2
4
  require 'yard'
3
5
 
4
6
  namespace :test do
5
7
  Rake::TestTask.new(:unit) do |t|
6
8
  t.libs = ['lib']
7
- t.ruby_opts += ["-w -Itest"]
9
+ t.ruby_opts += ['-w -Itest']
8
10
  t.test_files = FileList['test/gir_ffi-pretty_printer/*_test.rb']
9
11
  t.warning = true
10
12
  end
11
13
 
12
- Rake::TestTask.new(:end_to_end) do |t|
14
+ Rake::TestTask.new(:integration) do |t|
13
15
  t.libs = ['lib']
14
- t.ruby_opts += ["-w -Itest"]
15
- t.test_files = FileList['test/end_to_end/*_test.rb']
16
+ t.ruby_opts += ['-w -Itest']
17
+ t.test_files = FileList['test/integration/*_test.rb']
16
18
  t.warning = true
17
19
  end
18
20
 
19
- task :all => [:unit, :end_to_end]
21
+ task all: [:unit, :integration]
20
22
  end
21
23
 
22
24
  YARD::Rake::YardocTask.new
23
25
 
24
- task :test => 'test:all'
25
-
26
- task :default => 'test'
26
+ task test: 'test:all'
27
27
 
28
+ task default: 'test'
@@ -0,0 +1,84 @@
1
+ class GirFFI::ClassPrettyPrinter
2
+ def initialize(klass)
3
+ @klass = klass
4
+ @printed_instance_methods = []
5
+ end
6
+
7
+ def pretty_print
8
+ arr = []
9
+ arr << "class #{@klass.name} < #{@klass.superclass.name}"
10
+ arr << pretty_print_singleton_methods.indent
11
+ arr << pretty_print_instance_methods.indent
12
+ arr << 'end'
13
+ arr.join "\n"
14
+ end
15
+
16
+ private
17
+
18
+ def pretty_print_instance_methods
19
+ arr =
20
+ instance_method_list.map { |mth| pretty_print_method(mth) } +
21
+ instance_method_list.map { |mth| pretty_print_alias(mth) }
22
+ arr.compact.join "\n"
23
+ end
24
+
25
+ def instance_method_name_list
26
+ @klass.instance_methods(false).sort
27
+ end
28
+
29
+ def instance_method_list
30
+ @instance_method_list ||=
31
+ instance_method_name_list.map { |name| @klass.instance_method name }
32
+ end
33
+
34
+ def pretty_print_alias(meth)
35
+ if meth.name != meth.original_name
36
+ "alias_method '#{meth.name}', '#{meth.original_name}'"
37
+ end
38
+ end
39
+
40
+ def pretty_print_method(meth)
41
+ return if instance_method_printed? meth.original_name
42
+ @printed_instance_methods << meth.original_name
43
+ method_source meth
44
+ end
45
+
46
+ def method_source(meth)
47
+ if meth.arity == -1
48
+ unless @klass.setup_instance_method meth.name.to_s
49
+ @klass.setup_instance_method ''
50
+ end
51
+
52
+ meth = @klass.instance_method meth.name
53
+ end
54
+
55
+ begin
56
+ meth.to_ruby
57
+ rescue => e
58
+ warn "Printing #{@klass.name}##{meth.name} failed: #{e.message}"
59
+ end
60
+ end
61
+
62
+ def instance_method_printed?(mname)
63
+ @printed_instance_methods.include? mname
64
+ end
65
+
66
+ def pretty_print_singleton_methods
67
+ arr = []
68
+ @klass.methods(false).sort.each do |mname|
69
+ meth = @klass.method mname
70
+
71
+ if meth.arity == -1
72
+ @klass.setup_method mname.to_s
73
+ meth = @klass.method mname
74
+ end
75
+
76
+ begin
77
+ arr << meth.to_ruby
78
+ rescue => e
79
+ warn "Printing #{@klass.name}.#{mname} failed: #{e.message}"
80
+ end
81
+ end
82
+ arr.join "\n"
83
+ end
84
+ end
@@ -2,57 +2,15 @@ require 'live_ast/full'
2
2
  require 'live_ast/to_ruby'
3
3
  require 'gir_ffi'
4
4
  require 'indentation'
5
+ require 'gir_ffi-pretty_printer/class_pretty_printer'
5
6
 
6
7
  class GirFFI::PrettyPrinter
7
- def pretty_print_class klass
8
- arr = []
9
- arr << "class #{klass.name} < #{klass.superclass.name}"
10
- arr << pretty_print_singleton_methods(klass).indent
11
- arr << pretty_print_instance_methods(klass).indent
12
- arr << "end"
13
- arr.join "\n"
14
- end
15
-
16
- def pretty_print_instance_methods klass
17
- arr = []
18
- klass.instance_methods(false).each do |mname|
19
- meth = klass.instance_method mname
20
-
21
- if meth.arity == -1
22
- klass.setup_instance_method mname.to_s
23
- meth = klass.instance_method mname
24
- end
25
-
26
- begin
27
- arr << meth.to_ruby
28
- rescue => e
29
- warn "Printing #{klass.name}##{mname} failed: #{e.message}"
30
- end
31
- end
32
- arr.join "\n"
33
- end
34
-
35
- def pretty_print_singleton_methods klass
36
- arr = []
37
- klass.methods(false).each do |mname|
38
- meth = klass.method mname
39
-
40
- if meth.arity == -1
41
- klass.setup_method mname.to_s
42
- meth = klass.method mname
43
- end
44
-
45
- begin
46
- arr << meth.to_ruby
47
- rescue => e
48
- warn "Printing #{klass.name}.#{mname} failed: #{e.message}"
49
- end
50
- end
51
- arr.join "\n"
8
+ def pretty_print_class(klass)
9
+ GirFFI::ClassPrettyPrinter.new(klass).pretty_print
52
10
  end
53
11
 
54
- def pretty_print_function modul, mname
55
- str = ""
12
+ def pretty_print_function(modul, mname)
13
+ str = ''
56
14
  begin
57
15
  modul.setup_method mname.to_s unless modul.respond_to? mname
58
16
 
@@ -64,12 +22,15 @@ class GirFFI::PrettyPrinter
64
22
  str
65
23
  end
66
24
 
67
- def pretty_print_constant modul, const_name
25
+ def pretty_print_constant(modul, const_name)
68
26
  value = modul.const_get(const_name)
69
27
  "#{const_name} = #{value.inspect}"
28
+ rescue => e
29
+ warn "Printing #{modul.name}.#{const_name} failed: #{e.message}"
30
+ ''
70
31
  end
71
32
 
72
- def pretty_print module_name, version = nil
33
+ def pretty_print(module_name, version = nil)
73
34
  GirFFI.setup module_name, version
74
35
  arr = []
75
36
  gir = GObjectIntrospection::IRepository.default
@@ -81,12 +42,11 @@ class GirFFI::PrettyPrinter
81
42
  infos.each do |info|
82
43
  case info.info_type
83
44
  when :struct, :object
84
- # FIXME: We skip IConv for now since building it fails.
85
- if info.name == "IConv"
86
- warn "Skipping #{module_name}::#{info.name}"
87
- else
45
+ begin
88
46
  klass = GirFFI::Builder.build_class info
89
47
  arr << pretty_print_class(klass).indent
48
+ rescue SyntaxError
49
+ warn "Skipping #{module_name}::#{info.name}: build failed"
90
50
  end
91
51
  when :constant
92
52
  arr << pretty_print_constant(modul, info.name).indent
@@ -97,7 +57,7 @@ class GirFFI::PrettyPrinter
97
57
  end
98
58
  end
99
59
 
100
- arr << "end"
60
+ arr << "end\n"
101
61
  arr.join "\n"
102
62
  end
103
63
  end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class SimpleClass
4
+ def bar
5
+ 'hello'
6
+ end
7
+ end
8
+
9
+ class SimpleAlias
10
+ def bar
11
+ 'hello'
12
+ end
13
+
14
+ alias_method :foo, :bar
15
+ end
16
+
17
+ class ComplexAlias
18
+ def baz
19
+ 'original'
20
+ end
21
+
22
+ alias_method :baz_without_qux, :baz
23
+
24
+ def baz_with_qux
25
+ baz_without_qux + '-more'
26
+ end
27
+
28
+ alias_method :baz, :baz_with_qux
29
+ end
30
+
31
+ describe GirFFI::ClassPrettyPrinter do
32
+ let(:instance) { GirFFI::ClassPrettyPrinter.new klass }
33
+
34
+ describe '#pretty_print' do
35
+ describe 'for a class with a simple method definition' do
36
+ let(:klass) { SimpleClass }
37
+ it 'pretty-prints the class' do
38
+ expected = <<-END.reset_indentation.chomp
39
+ class SimpleClass < Object
40
+
41
+ def bar
42
+ "hello"
43
+ end
44
+ end
45
+ END
46
+
47
+ result = instance.pretty_print
48
+ result.must_equal expected
49
+ end
50
+ end
51
+
52
+ describe 'for a class with a simple aliased method' do
53
+ let(:klass) { SimpleAlias }
54
+ it 'pretty-prints the class' do
55
+ expected = <<-END.reset_indentation.chomp
56
+ class SimpleAlias < Object
57
+
58
+ def bar
59
+ "hello"
60
+ end
61
+ alias_method 'foo', 'bar'
62
+ end
63
+ END
64
+
65
+ result = instance.pretty_print
66
+ result.must_equal expected
67
+ end
68
+ end
69
+
70
+ describe 'for a class with an alias chain' do
71
+ let(:klass) { ComplexAlias }
72
+ it 'pretty-prints the class' do
73
+ expected = <<-END.reset_indentation.chomp
74
+ class ComplexAlias < Object
75
+
76
+ def baz_with_qux
77
+ (baz_without_qux + "-more")
78
+ end
79
+ def baz
80
+ "original"
81
+ end
82
+ alias_method 'baz', 'baz_with_qux'
83
+ alias_method 'baz_without_qux', 'baz'
84
+ end
85
+ END
86
+
87
+ result = instance.pretty_print
88
+ result.must_equal expected
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,15 +1,10 @@
1
1
  require 'test_helper'
2
2
  describe GirFFI::PrettyPrinter do
3
- describe "#initialize" do
4
- it "takes no arguments" do
5
- GirFFI::PrettyPrinter.new
6
- end
7
- end
8
-
9
- describe "#pretty_print" do
3
+ describe '#pretty_print' do
10
4
  let(:instance) { GirFFI::PrettyPrinter.new }
11
- it "pretty-prints a module without type specification" do
5
+ it 'pretty-prints a module without version specification' do
12
6
  instance.pretty_print 'GObject'
7
+ pass
13
8
  end
14
9
  end
15
10
  end