lib/spoom/coverage/snapshot.rb in spoom-1.1.11 vs lib/spoom/coverage/snapshot.rb in spoom-1.1.12

- old
+ new

@@ -30,48 +30,53 @@ def print(out: $stdout, colors: true, indent_level: 0) printer = SnapshotPrinter.new(out: out, colors: colors, indent_level: indent_level) printer.print_snapshot(self) end - sig { params(json: String).returns(Snapshot) } - def self.from_json(json) - from_obj(JSON.parse(json)) + sig { params(arg: T.untyped).returns(String) } + def to_json(*arg) + serialize.to_json(*arg) end - sig { params(obj: T::Hash[String, T.untyped]).returns(Snapshot) } - def self.from_obj(obj) - snapshot = Snapshot.new - snapshot.timestamp = obj.fetch("timestamp", 0) - snapshot.version_static = obj.fetch("version_static", nil) - snapshot.version_runtime = obj.fetch("version_runtime", nil) - snapshot.duration = obj.fetch("duration", 0) - snapshot.commit_sha = obj.fetch("commit_sha", nil) - snapshot.commit_timestamp = obj.fetch("commit_timestamp", nil) - snapshot.files = obj.fetch("files", 0) - snapshot.rbi_files = obj.fetch("rbi_files", 0) - snapshot.modules = obj.fetch("modules", 0) - snapshot.classes = obj.fetch("classes", 0) - snapshot.singleton_classes = obj.fetch("singleton_classes", 0) - snapshot.methods_with_sig = obj.fetch("methods_with_sig", 0) - snapshot.methods_without_sig = obj.fetch("methods_without_sig", 0) - snapshot.calls_typed = obj.fetch("calls_typed", 0) - snapshot.calls_untyped = obj.fetch("calls_untyped", 0) + class << self + extend T::Sig - sigils = obj.fetch("sigils", {}) - if sigils - Snapshot::STRICTNESSES.each do |strictness| - next unless sigils.key?(strictness) - snapshot.sigils[strictness] = sigils[strictness] - end + sig { params(json: String).returns(Snapshot) } + def from_json(json) + from_obj(JSON.parse(json)) end - snapshot - end + sig { params(obj: T::Hash[String, T.untyped]).returns(Snapshot) } + def from_obj(obj) + snapshot = Snapshot.new + snapshot.timestamp = obj.fetch("timestamp", 0) + snapshot.version_static = obj.fetch("version_static", nil) + snapshot.version_runtime = obj.fetch("version_runtime", nil) + snapshot.duration = obj.fetch("duration", 0) + snapshot.commit_sha = obj.fetch("commit_sha", nil) + snapshot.commit_timestamp = obj.fetch("commit_timestamp", nil) + snapshot.files = obj.fetch("files", 0) + snapshot.rbi_files = obj.fetch("rbi_files", 0) + snapshot.modules = obj.fetch("modules", 0) + snapshot.classes = obj.fetch("classes", 0) + snapshot.singleton_classes = obj.fetch("singleton_classes", 0) + snapshot.methods_with_sig = obj.fetch("methods_with_sig", 0) + snapshot.methods_without_sig = obj.fetch("methods_without_sig", 0) + snapshot.calls_typed = obj.fetch("calls_typed", 0) + snapshot.calls_untyped = obj.fetch("calls_untyped", 0) - sig { params(arg: T.untyped).returns(String) } - def to_json(*arg) - serialize.to_json(*arg) + sigils = obj.fetch("sigils", {}) + if sigils + Snapshot::STRICTNESSES.each do |strictness| + next unless sigils.key?(strictness) + + snapshot.sigils[strictness] = sigils[strictness] + end + end + + snapshot + end end end class SnapshotPrinter < Spoom::Printer extend T::Sig @@ -117,17 +122,19 @@ sig { params(hash: T::Hash[String, Integer], total: Integer).void } def print_map(hash, total) indent hash.each do |key, value| next unless value > 0 + printl("#{key}: #{value}#{percent(value, total)}") end dedent end sig { params(value: T.nilable(Integer), total: T.nilable(Integer)).returns(String) } def percent(value, total) return "" if value.nil? || total.nil? || total == 0 + " (#{(value.to_f * 100.0 / total.to_f).round}%)" end end end end