Permalink
Browse files

Add YARD.windows?, YARD.ruby18? and YARD.ruby19?

Deprecate RUBY18 and RUBY19 constant usage and replace constants with ruby18? and ruby19? calls
  • Loading branch information...
1 parent e72ae7c commit 706c9f5f325f3f722f4c59db4e515da027499ea9 @lsegal committed Aug 6, 2012
View
@@ -27,10 +27,30 @@ def self.parse_string(*args) Parser::SourceParser.parse_string(*args) end
# (see YARD::Config.load_plugins)
# @deprecated Use {Config.load_plugins}
def self.load_plugins; YARD::Config.load_plugins end
+
+ # @return [Boolean] whether YARD is being run inside of Windows
+ def self.windows?
+ return @windows if defined? @windows
+ require 'rbconfig'
+ if ::RbConfig::CONFIG['host_os'] =~ /mingw|win32|cygwin/
+ @wnidows = true
+ else
+ @windows = false
+ end
+ ensure
+ @windows ||= false
+ end
+
+ # @return [Boolean] whether YARD is being run in Ruby 1.8 mode
+ def self.ruby18?; !ruby19? end
+
+ # @return [Boolean] whether YARD is being run in Ruby 1.9 mode
+ def self.ruby19?; @ruby19 ||= (RUBY_VERSION >= "1.9.1") end
end
# Keep track of Ruby version for compatibility code
-RUBY19, RUBY18 = *(RUBY_VERSION >= "1.9.1" ? [true, false] : [false, true])
+# @deprecated Use {YARD.ruby18?} or {YARD.ruby19?} instead.
+RUBY18, RUBY19 = YARD.ruby18?, YARD.ruby19?
# Load Ruby core extension classes
Dir.glob(File.join(YARD::ROOT, 'yard', 'core_ext', '*.rb')).each do |file|
@@ -509,11 +509,12 @@ def signature(meth, link = true, show_extras = true, full_attr_name = true)
# @return [String] the current character set
# @since 0.5.4
def charset
- if @file && RUBY19
+ has_encoding = defined?(::Encoding)
+ if @file && has_encoding
lang = @file.contents.encoding.to_s
else
- return 'utf-8' unless RUBY19 || lang = ENV['LANG']
- if RUBY19
+ return 'utf-8' unless has_encoding || lang = ENV['LANG']
+ if has_encoding
lang = ::Encoding.default_external.name.downcase
else
lang = lang.downcase.split('.').last
@@ -75,7 +75,7 @@
describe YARD::CodeObjects, "BUILTIN_MODULES" do
it "should include all base modules" do
YARD::CodeObjects::BUILTIN_MODULES.each do |name|
- next if RUBY19 && ["Precision"].include?(name)
+ next if YARD.ruby19? && ["Precision"].include?(name)
eval(name).should be_instance_of(Module)
end
end
@@ -71,20 +71,20 @@
data.force_encoding('binary')
file = ExtraFileObject.new('file.txt', data)
['Shift_JIS', 'Windows-31J'].should include(file.contents.encoding.to_s)
- end if RUBY19
+ end if YARD.ruby19?
it "should warn if @encoding is invalid" do
log.should_receive(:warn).with("Invalid encoding `INVALID' in file.txt")
data = "# @encoding INVALID\nFOO"
encoding = data.encoding
file = ExtraFileObject.new('file.txt', data)
file.contents.encoding.should == encoding
- end if RUBY19
+ end if YARD.ruby19?
it "should ignore encoding in 1.8.x (or encoding-unaware platforms)" do
log.should_not_receive(:warn)
file = ExtraFileObject.new('file.txt', "# @encoding INVALID\nFOO")
- end if RUBY18
+ end if YARD.ruby18?
it "should attempt to re-parse data as 8bit ascii if parsing fails" do
log.should_not_receive(:warn)
@@ -546,7 +546,7 @@ class Foo < Bar
parser = Parser::SourceParser.new
File.should_receive(:read_binary).with('tmpfile').and_return(src)
result = parser.parse("tmpfile")
- if HAVE_RIPPER && RUBY19
+ if HAVE_RIPPER && YARD.ruby19?
['Shift_JIS', 'Windows-31J', 'UTF-8'].send(msg, include(
result.enumerator[0].source.encoding.to_s))
end
@@ -572,7 +572,7 @@ class Foo < Bar
['UTF-8'].send(msg, include(content.encoding.to_s))
end
end
- end if RUBY19
+ end if YARD.ruby19?
Parser::SourceParser::ENCODING_BYTE_ORDER_MARKS.each do |encoding, bom|
it "should understand #{encoding.upcase} BOM" do
@@ -584,7 +584,7 @@ class Foo < Bar
Registry.all(:class).first.path.should == "FooBar"
result.enumerator[0].source.encoding.to_s.downcase.should == encoding
end
- end if HAVE_RIPPER && RUBY19
+ end if HAVE_RIPPER && YARD.ruby19?
end
describe '#parse_in_order' do
@@ -22,22 +22,22 @@ def options
describe '#charset' do
it "should return foo if LANG=foo" do
- ENV.should_receive(:[]).with('LANG').and_return('shift_jis') if RUBY18
+ ENV.should_receive(:[]).with('LANG').and_return('shift_jis') if YARD.ruby18?
Encoding.default_external.should_receive(:name).and_return('shift_jis') if defined?(Encoding)
charset.should == 'shift_jis'
end
['US-ASCII', 'ASCII-7BIT', 'ASCII-8BIT'].each do |type|
it "should convert #{type} to iso-8859-1" do
- ENV.should_receive(:[]).with('LANG').and_return(type) if RUBY18
+ ENV.should_receive(:[]).with('LANG').and_return(type) if YARD.ruby18?
Encoding.default_external.should_receive(:name).and_return(type) if defined?(Encoding)
charset.should == 'iso-8859-1'
end
end
it "should support utf8 as an encoding value for utf-8" do
type = 'utf8'
- ENV.should_receive(:[]).with('LANG').and_return(type) if RUBY18
+ ENV.should_receive(:[]).with('LANG').and_return(type) if YARD.ruby18?
Encoding.default_external.should_receive(:name).and_return(type) if defined?(Encoding)
charset.should == 'utf-8'
end
@@ -46,15 +46,15 @@ def options
@file = OpenStruct.new(:contents => 'foo'.force_encoding('sjis'))
# not the correct charset name, but good enough
['Shift_JIS', 'Windows-31J'].should include(charset)
- end if RUBY19
+ end if YARD.ruby19?
it "should take file encoding if there is a file" do
- ENV.stub!(:[]).with('LANG').and_return('utf-8') if RUBY18
+ ENV.stub!(:[]).with('LANG').and_return('utf-8') if YARD.ruby18?
@file = OpenStruct.new(:contents => 'foo')
charset.should == 'utf-8'
- end if RUBY18
+ end if YARD.ruby18?
- if RUBY18
+ if YARD.ruby18?
it "should return utf-8 if no LANG env is set" do
ENV.should_receive(:[]).with('LANG').and_return(nil)
charset.should == 'utf-8'

0 comments on commit 706c9f5

Please sign in to comment.