1# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14# frozen_string_literal: true 15 16# 17# Huawei Technologies Co.,Ltd. 18require 'optparse' 19require 'yaml' 20require 'erb' 21 22class FullMd 23 attr_accessor :full, :properties_hash, :exceptions_hash, :verification_hash 24 25 def initialize(spec) 26 @template_file = File.join(__dir__, 'templates', 'full_md.erb') 27 @full = spec 28 @exceptions_hash = convert_to_hash(@full['exceptions']) 29 @properties_hash = convert_to_hash(@full['properties']) 30 @verification_hash = convert_to_hash(@full['verification']) 31 end 32 33 def generate(file) 34 File.open(file, 'w+') do |f| 35 f.write(render) 36 end 37 end 38 39 private 40 41 def format_array(instr) 42 instr.nil? ? '' : instr['format'].join(', ').gsub(/_/, '\\_') 43 end 44 45 def covered_description(desc) 46 non_testable = desc['non_testable'] ? ' - Non-testable' : '' 47 "#{md(desc['assertion'])} [#{test_list(desc['tests'])}]#{non_testable}" 48 end 49 50 def props(properties) 51 "[#{md(properties.join(', '))}]" 52 end 53 54 def verification_entry(ver) 55 "#{md(@verification_hash[ver['verification']])} [#{test_list(ver['tests'])}]" 56 end 57 58 def exception_entry(entry) 59 "#{md(@exceptions_hash[entry['exception']])} [#{test_list(entry['tests'])}]" 60 end 61 62 def test_list(tests) 63 tests.any? ? tests.join(', ') : '\`not covered\`' 64 end 65 66 def md(str) 67 str.gsub(/_/, '\\_').gsub(/\n/, ' ').rstrip 68 end 69 70 def render 71 @template = File.read(@template_file) 72 ERB.new(@template, nil, '%-').result(binding) 73 end 74 75 def convert_to_hash(arr) 76 arr.map { |i| [i['tag'], i['description']] }.to_h 77 end 78end 79