Skip to content

Commit

Permalink
dev: docx coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
askonev committed Nov 10, 2023
1 parent a9647c4 commit 24d3249
Show file tree
Hide file tree
Showing 2 changed files with 1,981 additions and 0 deletions.
103 changes: 103 additions & 0 deletions lib/coverage/method_coverage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# frozen_string_literal: true

require 'net/http'
require 'json'

FOLDER = File.join(Dir.pwd, 'reports').to_s

EXIST = 'AddText'
NONEXIST = 'GetDropdownList'

COVERAGE_DOCX = 'reports/text_document_api.json'
COVERAGE_XLSX = 'reports/spreadsheet_api.json'
COVERAGE_PPTX = 'reports/presentation_api.json'

SOURCE_DOCX = File.join(Dir.pwd, 'js', 'docx').to_s
SOURCE_XSLX = File.join(Dir.pwd, 'js', 'xlsx').to_s
SOURCE_PPTX = File.join(Dir.pwd, 'js', 'pptx').to_s

BUILDER_API = 'https://raw.githubusercontent.com/ONLYOFFICE-QA/testing-api.onlyoffice.com/master/templates/document_builder/usage_api.json'

# MethodCoverage class
class Matcher

def initialize(pattern, path)
@flag = false
@pattern = pattern
@path = path
recursive_search
end

def pattern_found?
@flag
end

def recursive_search(pattern = @pattern, node = @path)
if File.file?(node)
contains_matches?(node, pattern)
return nil
end

dir_instance = Dir.new(node)
dir_instance.each_child do |child|
recursive_search(pattern, File.join(dir_instance.path.to_s, child))
end
end

def contains_matches?(path, pattern)
# result = false
File.open(path, 'r') do |file|
file.each_line do |line|
next unless line.include?(pattern)

@flag = true # Switch global flag
break
end
end
end
end

def __get_api_methods(url)
file_url = URI.parse(url)
http = Net::HTTP.new(file_url.host.to_s, file_url.port)
http.use_ssl = (file_url.scheme = 'https')
response = http.get(file_url.path.to_s)
if response.code.to_i == 200
response.body
else
puts "Failed to download the file. HTTP status code: #{response.code}"
end
end

def __write_result(json = __get_api_methods(BUILDER_API))
File.binwrite(File.join(FOLDER, 'json'), json)
# File.binwrite(COVERAGE_DOCX, JSON.pretty_generate(json['Text document API']))
# File.binwrite(COVERAGE_XLSX, JSON.pretty_generate(json['Spreadsheet API']))
# File.binwrite(COVERAGE_PPTX, JSON.pretty_generate(json['Presentation API']))
end

def __read_json
JSON.parse(File.read(File.join(FOLDER, 'json')))
end

def __coverage(json, source)
array = []

json.each do |node|
methods = node[1]
methods.each do |method|
unless Matcher.new(method, source).pattern_found?
p method
array << method
end
end
end
array
end

result = __coverage(__read_json['Text document API'], SOURCE_DOCX)
p result


# result = Matcher.new(NONEXIST, SOURCE_DOCX).pattern_found?
# p result
Loading

0 comments on commit 24d3249

Please sign in to comment.