aboutsummaryrefslogtreecommitdiff
path: root/reporting_test.cc
blob: f41db1b5b9d7a8123850e9109e76c06a5e41b841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- mode: C++ -*-
//
// Copyright 2023 Google LLC
//
// Licensed under the Apache License v2.0 with LLVM Exceptions (the
// "License"); you may not use this file except in compliance with the
// License.  You may obtain a copy of the License at
//
//     https://llvm.org/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Siddharth Nayyar

#include "reporting.h"

#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>

#include <catch2/catch.hpp>
#include "fidelity.h"

namespace stg {
namespace {

std::string filename_to_path(const std::string& f) {
  return std::filesystem::path("testdata") / f;
}

TEST_CASE("fidelity diff") {
  stg::FidelityDiff diff = {
      .symbol_transitions =
          {
              {{SymbolFidelity::TYPED, SymbolFidelity::UNTYPED},
               {"symbol1", "symbol2"}},
              {{SymbolFidelity::UNTYPED, SymbolFidelity::TYPED}, {"symbol3"}},
              {{SymbolFidelity::ABSENT, SymbolFidelity::UNTYPED},
               {"symbol4", "symbol5"}},
              {{SymbolFidelity::ABSENT, SymbolFidelity::TYPED}, {"symbol6"}},
              {{SymbolFidelity::TYPED, SymbolFidelity::ABSENT},
               {"symbol7", "symbol8"}},
              {{SymbolFidelity::UNTYPED, SymbolFidelity::ABSENT}, {"symbol9"}},
          },
      .type_transitions =
          {{{TypeFidelity::FULLY_DEFINED, TypeFidelity::ABSENT},
            {"struct s1", "union u1"}},
           {{TypeFidelity::DECLARATION_ONLY, TypeFidelity::ABSENT},
            {"struct s2"}},
           {{TypeFidelity::FULLY_DEFINED, TypeFidelity::DECLARATION_ONLY},
            {"enum e1"}},
           {{TypeFidelity::ABSENT, TypeFidelity::DECLARATION_ONLY},
            {"union u2"}},
           {{TypeFidelity::DECLARATION_ONLY, TypeFidelity::FULLY_DEFINED},
            {"enum e2", "union u3"}},
           {{TypeFidelity::ABSENT, TypeFidelity::FULLY_DEFINED},
            {"struct s3"}}},
  };

  std::ostringstream report;
  CHECK(reporting::FidelityDiff(diff, report));

  std::ifstream expected_report_file(filename_to_path("fidelity_diff_report"));
  std::ostringstream expected_report;
  expected_report << expected_report_file.rdbuf();
  CHECK(report.str() == expected_report.str());
}

}  // namespace
}  // namespace stg