Commit 17dce301 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

update the 'graphics_text_align' unit test

parent ef5bc239
Loading
Loading
Loading
Loading
+0 −120
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * 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.
 */
#include "graphics/brush.h"
#include "graphics/color.h"
#include "graphics/layer.h"
#include "graphics/text.h"
#include "../unittest.h"

using namespace fviz;

Status drawTestAt(Layer* l, uint32_t x, uint32_t y, TextStyle s) {
  StrokeStyle ss;
  strokeLine(l, x - 10, y, x + 10, y, ss);
  strokeLine(l, x, y - 10, x, y + 10, ss);
  drawText("Ijsselmeerdijk", x, y, s, l);
}

int main(int argc, char** argv) {
  Layer l(800, 500);
  l.clear(Color{1, 1, 1, 1});


  {
    TextStyle s;
    //s.halign = TextHAlign::LEFT;
    //s.valign = TextVAlign::BASELINE;
    drawTestAt(&l, 100,  100, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::CENTER;
    //s.valign = TextVAlign::BASELINE;
    drawTestAt(&l, 400,  100, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::RIGHT;
    //s.valign = TextVAlign::BASELINE;
    drawTestAt(&l, 700,  100, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::LEFT;
    //s.valign = TextVAlign::TOP;
    drawTestAt(&l, 100,  200, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::CENTER;
    //s.valign = TextVAlign::TOP;
    drawTestAt(&l, 400,  200, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::RIGHT;
    //s.valign = TextVAlign::TOP;
    drawTestAt(&l, 700,  200, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::LEFT;
    //s.valign = TextVAlign::MIDDLE;
    drawTestAt(&l, 100,  300, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::CENTER;
    //s.valign = TextVAlign::MIDDLE;
    drawTestAt(&l, 400,  300, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::RIGHT;
    //s.valign = TextVAlign::MIDDLE;
    drawTestAt(&l, 700,  300, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::LEFT;
    //s.valign = TextVAlign::BOTTOM;
    drawTestAt(&l, 100,  400, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::CENTER;
    //s.valign = TextVAlign::BOTTOM;
    drawTestAt(&l, 400,  400, s);
  }

  {
    TextStyle s;
    //s.halign = TextHAlign::RIGHT;
    //s.valign = TextVAlign::BOTTOM;
    drawTestAt(&l, 700,  400, s);
  }

  CHECK_RC(l.writeToFile(std::string(argv[0]) + ".png"));
}
−9.81 KiB
Loading image diff...
+75 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * 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.
 */
#include "graphics/brush.h"
#include "graphics/color.h"
#include "graphics/layer.h"
#include "graphics/layer_svg.h"
#include "graphics/text.h"
#include "environment.h"
#include "utils/fileutil.h"
#include "unittest.h"

using namespace fviz;

void draw_test(
    const Environment& env,
    Layer* l,
    uint32_t x,
    uint32_t y,
    HAlign ax,
    VAlign ay) {
  TextStyle ts;
  ts.font = env.font;
  ts.font_size = from_unit(12);

  StrokeStyle ss;
  ss.line_width = from_unit(1);

  strokeLine(l, Point(x - 10, y), Point(x + 10, y), ss);
  strokeLine(l, Point(x, y - 10), Point(x, y + 10), ss);
  drawTextLabel("Ijsselmeerdijk", Point(x, y), ax, ay, ts, l);
}

int main(int argc, char** argv) {
  Environment env;
  EXPECT_OK(environment_setup_defaults(&env));

  LayerRef layer;
  auto rc = layer_bind_svg(
      800,
      500,
      96,
      from_unit(12),
      Color::fromRGB(1.0, 1.0, 1.0),
      [&] (auto svg) {
        FileUtil::write(std::string(argv[0]) + ".svg", Buffer(svg.data(), svg.size()));
        return OK;
      },
      &layer);

  draw_test(env, layer.get(), 100,  100, HAlign::LEFT, VAlign::TOP);
  draw_test(env, layer.get(), 400,  100, HAlign::CENTER, VAlign::TOP);
  draw_test(env, layer.get(), 700,  100, HAlign::RIGHT, VAlign::TOP);

  draw_test(env, layer.get(), 100,  200, HAlign::LEFT, VAlign::CENTER);
  draw_test(env, layer.get(), 400,  200, HAlign::CENTER, VAlign::CENTER);
  draw_test(env, layer.get(), 700,  200, HAlign::RIGHT, VAlign::CENTER);

  draw_test(env, layer.get(), 100,  300, HAlign::LEFT, VAlign::BOTTOM);
  draw_test(env, layer.get(), 400,  300, HAlign::CENTER, VAlign::BOTTOM);
  draw_test(env, layer.get(), 700,  300, HAlign::RIGHT, VAlign::BOTTOM);

  layer_submit(layer.get());
}
+1 −19
Original line number Diff line number Diff line
@@ -16,28 +16,10 @@
#include <unistd.h>
#include <iostream>
#include <sexpr_parser.h>
#include "unittest.h"

using namespace fviz;

#define EXPECT(X) \
    if (!(X)) { \
      std::cerr << "ERROR: expectation failed: " << #X << " on line " << __LINE__ <<  std::endl; \
      std::exit(1); \
    }

#define EXPECT_EQ(A, B) EXPECT((A) == (B))

#define EXPECT_STREQ(A, B) EXPECT(std::string(A) == std::string(B))

#define EXPECT_OK(X) \
    do { \
      auto rc = (X); \
      if (!rc) { \
        std::cerr << "ERROR: " << rc.message << " on line " << __LINE__ <<  std::endl; \
        std::exit(1); \
      } \
    } while(0)

void test_parse_literals() {
  std::string confstr =
      R"(1337 hello world)";
+12 −219
Original line number Diff line number Diff line
@@ -11,8 +11,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _fviz_UTIL_UNITTEST_H
#define _fviz_UTIL_UNITTEST_H
#pragma once

#include <functional>
#include <unordered_map>
@@ -20,230 +19,24 @@
#include <string>
#include <string.h>

#include "utils/exception.h"
#include "utils/fileutil.h"
#include "utils/inputstream.h"
#include "utils/outputstream.h"
#include "utils/inspect.h"
#include "utils/random.h"
#include "return_code.h"

const char kExpectationFailed[] = "ExpectationFailed";

#define UNIT_TEST(T) \
    static fviz::test::UnitTest T(#T); \
    int main() { \
      auto& t = T; \
      return t.run(); \
    }

#define TEST_CASE(T, N, L) \
    static fviz::test::UnitTest::TestCase __##T##__case__##N(&T, #N, (L));

#define TEST_INITIALIZER(T, N, L) \
    static fviz::test::UnitTest::TestInitializer __##T##__case__##N( \
        &T, (L));

#define EXPECT(X) \
    if (!(X)) { \
      RAISE( \
          kExpectationFailed, \
          "expectation failed: %s", #X); \
    }


void EXPECT_TRUE(bool val) {
  if (!val) {
    RAISE(
        kExpectationFailed,
        "expectation failed: expected TRUE, got FALSE");
  }
}

void EXPECT_FALSE(bool val) {
  if (val) {
    RAISE(
        kExpectationFailed,
        "expectation failed: expected FALSE, got TRUE");
  }
}

void CHECK_RC(fviz::Status rc) {
  if (rc != fviz::OK) {
    std::exit(1);
  }
}

void CHECK_RC(fviz::ReturnCode rc) {
  if (!rc) {
    RAISE(
        kExpectationFailed,
        rc.message);
  }
      std::cerr << "ERROR: expectation failed: " << #X << " on line " << __LINE__ <<  std::endl; \
      std::exit(1); \
    }

template <typename T1, typename T2>
void EXPECT_EQ(T1 left, T2 right) {
  if (!(left == right)) {
    RAISE(
        kExpectationFailed,
        "expectation failed: %s == %s",
        fviz::inspect<T1>(left).c_str(),
        fviz::inspect<T2>(right).c_str());
  }
}
#define EXPECT_EQ(A, B) EXPECT((A) == (B))

#define EXPECT_EXCEPTION(E, L) \
    { \
      bool raised = false; \
      try { \
        L(); \
      } catch (fviz::Exception e) { \
        raised = true; \
        auto msg = e.getMessage().c_str(); \
        if (strcmp(msg, E) != 0) { \
          RAISE( \
              kExpectationFailed, \
              "excepted exception '%s' but got '%s'", E, msg); \
        } \
      } \
      if (!raised) { \
        RAISE( \
            kExpectationFailed, \
            "excepted exception '%s' but got no exception", E); \
      } \
    }
#define EXPECT_STREQ(A, B) EXPECT(std::string(A) == std::string(B))

#define EXPECT_FILES_EQ(F1, F2) \
  { \
    auto one = fviz::FileInputStream::openFile(F1); \
    auto two = fviz::FileInputStream::openFile(F2); \
    std::string one_str; \
    std::string two_str; \
    one->readUntilEOF(&one_str); \
    two->readUntilEOF(&two_str); \
    if (one_str != two_str) { \
      std::string filename1(F1); \
      std::string filename2(F2); \
      RAISE( \
          kExpectationFailed, \
          "expected files '%s' and '%s' to be equal, but the differ", \
          filename1.c_str(), filename2.c_str()); \
#define EXPECT_OK(X) \
    do { \
      auto rc = (X); \
      if (!rc) { \
        std::cerr << "ERROR: " << rc.message << " on line " << __LINE__ <<  std::endl; \
        std::exit(1); \
      } \
  }


namespace fviz {
namespace test {

class UnitTest {
public:

  static std::string tempFilePath() {
    return "/tmp/_libfnord_test_tmp/";
  }

  static std::string testDataPath() {
    return "./";
  }
    } while(0)
  class TestCase {
  public:
    TestCase(
        UnitTest* test,
        const char* name,
        std::function<void ()> lambda) :
        name_(name),
        lambda_(lambda) {
      test->addTestCase(this);
    }

    const char* name_;
    std::function<void ()> lambda_;
  };

  class TestInitializer {
  public:
    TestInitializer(
        UnitTest* test,
        std::function<void ()> lambda) :
        lambda_(lambda) {
      test->addInitializer(this);
    }

    std::function<void ()> lambda_;
  };

  UnitTest(const char* name) : name_(name) {}

  void addTestCase(const TestCase* test_case) {
    cases_.push_back(test_case);
  }

  void addInitializer(const TestInitializer* init) {
    initializers_.push_back(init);
  }

  int run() {
    fviz::FileUtil::mkdir_p(UnitTest::tempFilePath());

    for (auto initializer : initializers_) {
      initializer->lambda_();
    }

    fprintf(stderr, "%s\n", name_);

    const TestCase* current_test_case = nullptr;
    int num_tests_passed = 0;
    std::unordered_map<const TestCase*, fviz::Exception> errors;

    for (auto test_case : cases_) {
      fprintf(stderr, "    %s::%s", name_, test_case->name_);
      fflush(stderr);
      current_test_case = test_case;

      try {
        test_case->lambda_();
      } catch (fviz::Exception e) {
        fprintf(stderr, " \033[1;31m[FAIL]\e[0m\n");
        errors.emplace(test_case, e);
        continue;
      }

      num_tests_passed++;
      fprintf(stderr, " \033[1;32m[PASS]\e[0m\n");
    }

    if (num_tests_passed != cases_.size()) {
      for (auto test_case : cases_) {
        const auto& err = errors.find(test_case);

        if (err != errors.end()) {
          fprintf(
              stderr,
              "\n\033[1;31m[FAIL] %s::%s\e[0m\n",
              name_,
              test_case->name_);
          err->second.debugPrint();
        }
      }

      fprintf(stderr, 
          "\n\033[1;31m[FAIL] %i/%i tests failed :(\e[0m\n",
          (int) cases_.size() - num_tests_passed,
          (int) cases_.size());
      return 1;
    }

    return 0;
  }

protected:
  const char* name_;
  std::vector<const TestCase*> cases_;
  std::vector<const TestInitializer*> initializers_;
};

}
}
#endif