Skip to content

Commit

Permalink
Merge pull request #18 from betacraft/RUB-10
Browse files Browse the repository at this point in the history
RUB-10 Add test case for ruby vernac parser
  • Loading branch information
ashish91 authored Oct 17, 2023
2 parents 6af3bac + 7e83426 commit 716ed06
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 34 deletions.
48 changes: 33 additions & 15 deletions lib/ruby-vernac-parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'forwardable'
require 'open3'

require_relative "./rubyvernac/parser/language_parser"
require_relative "./rubyvernac/parser/language_alias_loader"
Expand Down Expand Up @@ -26,48 +27,65 @@ def execute
translated_code = translate_code

write_to_temp_file(translated_code)
# running script
begin
output = `#{command} #{temp_file_path}`
rescue => err
print "Error running script- #{err}"
return
ensure
@file_handler.delete_file(temp_file_path)
end
print "Script Output - \n"
print "#{output}"

output = run_ruby_subprocess

puts "Script Output -"
puts "#{output}"
end

def parse
translated_code = translate_code

print "Parsed code -\n#{translated_code}"
translated_code
end

private

def translate_code
source = @file_handler.read_file(@source_file)
keywords = @yaml_handler.stringified_load_file(@keywords_file)
keywords = load_keywords(@keywords_file)

@parser.parse(
byte_string: source,
keywords: keywords,
language: language
language: @language
)
end

def load_keywords(keywords_file)
keywords = @yaml_handler.stringified_load_file(keywords_file)
inverted_keywords = {}

keywords.each do |keyword_in_eng, keyword_in_non_eng|
inverted_keywords[keyword_in_non_eng.to_s] = keyword_in_eng.to_s
end

inverted_keywords
end

def run_ruby_subprocess
stdout, stderr, status = Open3.capture3(command, temp_file_path)
@file_handler.delete_file(temp_file_path)

raise "Error running script - #{stderr}" if !status.success?

stdout
end

def write_to_temp_file(content)
@file_handler.write_to_file(temp_file_path, content)
end

def temp_file_path
"#{@source_file}.tmp"
filepath, extension = @source_file.split('.')

"#{filepath}.tmp.#{extension}"
end

def command
@command ||= case language
@command ||= case @language
when "ruby"
"ruby"
else
Expand Down
11 changes: 1 addition & 10 deletions lib/rubyvernac/parser/language_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@ module Rubyvernac
module Parser

class LanguageParser
attr_reader :byte_string, :keywords, :language

def self.run(byte_string: "", keywords: {}, language: "")
new(byte_string, keywords, language).run
end

def initialize(byte_string, keywords, language)
def initialize
@processed_string_buffer = ""
@keywords_found = 0
@class_names = []

@byte_string = byte_string
@keywords = keywords
@language = language
end

def parse(byte_string:, keywords:, language:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module CloudProvider

class StubbedTranslatorApi

def initialize(stubbed_translations: 'lib/templates/lib/rubyvernac/stubs')
def initialize(stubbed_translations: 'lib/rubyvernac/stubs')
@stubbed_translations = stubbed_translations
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class KeywordsTranslator

def initialize(lang_code: , translations_path:, filename:)
@lang_code = lang_code
if ENV['STUB_CLOUD_APIS'] == 'true'
@translator_api = Rubyvernac::CloudProvider::StubbedTranslatorApi.new
if ENV['STUBBED_TRANSLATIONS']
@translator_api = Rubyvernac::CloudProvider::StubbedTranslatorApi.new(stubbed_translations: ENV['STUBBED_TRANSLATIONS'])
else
@translator_api = Rubyvernac::CloudProvider::TranslatorApi.new
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def initialize(lang_code: , translations_path:, filename:)
@translations_path = translations_path + '/classes'
@filename = filename

if ENV['STUB_CLOUD_APIS'] == 'true'
@translator_api = CloudProvider::StubbedTranslatorApi.new
if ENV['STUBBED_TRANSLATIONS']
@translator_api = CloudProvider::StubbedTranslatorApi.new(stubbed_translations: ENV['STUBBED_TRANSLATIONS'])
else
@translator_api = CloudProvider::TranslatorApi.new
end
Expand Down
2 changes: 0 additions & 2 deletions ruby-vernac-parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Gem::Specification.new do |spec|
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.12'
spec.add_development_dependency 'rspec-expectations', '~> 3.12'

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
20 changes: 20 additions & 0 deletions spec/ruby_vernac_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
RSpec.describe RubyVernacParser do
subject do
RubyVernacParser.new(
source_file: Dir.pwd + '/spec/stubs/ruby_vernac_parser/example.rb',
keywords_file: Dir.pwd + '/spec/stubs/ruby_vernac_parser/keywords.yml'
)
end

describe "#parse" do
it "returns translated code from hindi language to english language" do
expect{ subject.parse }.to output("Parsed code -\ndef कितने_आदमी_थे\nputs \"२ सरकार।\"\nend\n\nकितने_आदमी_थे()\n").to_stdout
end
end

describe "#execute" do
it "" do
expect{ subject.execute }.to output("Script Output -\n२ सरकार।\n").to_stdout
end
end
end
5 changes: 5 additions & 0 deletions spec/stubs/ruby_vernac_parser/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
कार्य कितने_आदमी_थे
छापो "२ सरकार।"
समाप्त

कितने_आदमी_थे()
49 changes: 49 additions & 0 deletions spec/stubs/ruby_vernac_parser/keywords.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
BEGIN: शुरुआत
END: आखिर
alias: उपनाम
and: और
begin: शुरू
break: विराम
case: स्थिति
class: सांचा
def: कार्य
do: करो
else: वरना
elsif: फिरअगर
end: समाप्त
ensure: सुनिश्चित
false: झूठा
for: इसलिए
if: अगर
in: में
module: गुण
next: अगला
nil: कुछनहीं
not: नहीं
or: अथवा
puts: छापो
redo: फिरसेकरो
rescue: बचाव
retry: पुनःप्रयास
return: लौटा
self: स्वयं
super: ऊपर
then: फिर
true: सच्चा
unless: अगरनही
until: जबतक
when: जब
while: जिसबीच
yield: होनेदे
require: आवश्यक
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
2 changes: 1 addition & 1 deletion spec/templates/translator/keywords_translator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

describe "#generate_translations" do
before :all do
ENV['STUB_CLOUD_APIS'] = 'true'
ENV['STUBBED_TRANSLATIONS'] = 'lib/templates/lib/rubyvernac/stubs'
end

it "generates translations" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe "#generate_translations" do
before :all do
ENV['STUB_CLOUD_APIS'] = 'true'
ENV['STUBBED_TRANSLATIONS'] = 'lib/templates/lib/rubyvernac/stubs'
end

it "generates translations" do
Expand Down

0 comments on commit 716ed06

Please sign in to comment.