<commit_before>//
// RemotePhotoTool - remote camera control software
// Copyright (C) 2008-2015 <NAME>
//
/// \file TestLuaState.cpp Tests for Lua::State class
//

// includes
#include "stdafx.h"
#include "CppUnitTest.h"
#include "Lua.hpp"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace LuaScriptingUnitTest
{
   /// tests Lua::State class
   TEST_CLASS(TestLuaState)
   {
   public:
      /// tests LoadSourceString()
      TEST_METHOD(TestLoadSourceString)
      {
         Lua::State state;

         state.LoadSourceString(_T("-- comment test"));
         state.LoadSourceString(_T("function run() return 42; end"));

         // TODO check that run() exists instead of running the script
         state.CallFunction(_T("run"));
      }

      /// tests error handling
      TEST_METHOD(TestStateErrorHandling)
      {
         Lua::State state;
         state.LoadSourceString(_T("function run() error(); end"));

         try
         {
            state.CallFunction(_T("run"));
         }
         catch (const Lua::Exception&)
         {
            // ok
            return;
         }
         catch (...)
         {
            Assert::Fail(_T("must throw Lua::Exception and nothing else"));
         }

         Assert::Fail(_T("must throw exception"));
      }

      /// tests RequireLib() method to load built-in libraries
      TEST_METHOD(TestStateRequireLib)
      {
         // setup
         Lua::State state;

         // run
         state.RequireLib("coroutine");

         // check
         Lua::Value value = state.GetValue(_T("coroutine"));
         Assert::IsTrue(value.GetType() == Lua::Value::typeTable, _T("type must be table"));

         Lua::Table table = state.GetTable(_T("coroutine"));
         Lua::Value funcYield = table.GetValue(_T("yield"));

         Assert::IsTrue(funcYield.GetType() == Lua::Value::typeFunction, _T("type must be function"));
      }

      /// tests temporary nil value handling
      TEST_METHOD(TestValueNilTemporary)
      {
         // set up
         Lua::State state;

         // run
         for (int i = 0; i < 100; i++)
         {
            Lua::Value value = state.GetValue("not_init");
            Assert::IsTrue(Lua::Value::typeNil == value.GetType(), _T("type must be nil"));
         }

         // check
         // must not overflow stack
      }

      /// tests CallFunction(), without return parameters
      TEST_METHOD(TestCallFunctionNoRet)
      {
         Lua::State state;

         state.LoadSourceString(_T("function run() return; end"));

         // TODO check that run() exists
         std::vector<Lua::Value> vecRetvals = state.CallFunction(_T("run"));
         Assert::AreEqual<size_t>(0, vecRetvals.size(), _T("must have returned zero return values"));
      }

      /// tests CallFunction(), with single return parameter
      TEST_METHOD(TestCallFunctionRetval)
      {
         Lua::State state;

         state.LoadSourceString(_T("function run() return 42; end"));

         // TODO check that run() exists
         std::vector<Lua::Value> vecRetvals = state.CallFunction(_T("run"), 1);
         Assert::AreEqual<size_t>(1, vecRetvals.size(), _T("must have returned one return value"));

         const Lua::Value& value = vecRetvals[0];
         Assert::IsTrue(Lua::Value::typeNumber == value.GetType(), _T("type must be number"));
         // TODO check value returned
      }

      /// tests CallFunction(), with multiple return parameters
      TEST_METHOD(TestCallFunctionMultipleRetvals)
      {
         Lua::State state;

         state.LoadSourceString(_T("function test() return 42, 42==6*7, \"123abc\", nil; end"));

         std::vector<Lua::Value> vecRetvals = state.CallFunction(_T("test"), 4);
         Assert::AreEqual<size_t>(4, vecRetvals.size(), _T("must have returned 4 return values"));

         Assert::IsTrue(Lua::Value::typeNumber == vecRetvals[0].GetType(), _T("type must be number"));
         Assert::IsTrue(Lua::Value::typeBoolean == vecRetvals[1].GetType(), _T("type must be boolean"));
         Assert::IsTrue(Lua::Value::typeString == vecRetvals[2].GetType(), _T("type must be string"));
         Assert::IsTrue(Lua::Value::typeNil == vecRetvals[3].GetType(), _T("type must be nil"));
      }

      TEST_METHOD(TestAddTable)<commit_msg>removed TODOs from Lua unit tests<commit_after>//
// RemotePhotoTool - remote camera control software
// Copyright (C) 2008-2015 <NAME>
//
/// \file TestLuaState.cpp Tests for Lua::State class
//

// includes
#include "stdafx.h"
#include "CppUnitTest.h"
#include "Lua.hpp"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace LuaScriptingUnitTest
{
   /// tests Lua::State class
   TEST_CLASS(TestLuaState)
   {
   public:
      /// tests LoadSourceString()
      TEST_METHOD(TestLoadSourceString)
      {
         Lua::State state;

         state.LoadSourceString(_T("-- comment test"));
         state.LoadSourceString(_T("function run() return 42; end"));

         Assert::IsTrue(state.GetValue(_T("run")).GetType() == Lua::Value::typeFunction,
            _T("run must be of type function"));

         state.CallFunction(_T("run"));
      }

      /// tests error handling
      TEST_METHOD(TestStateErrorHandling)
      {
         Lua::State state;
         state.LoadSourceString(_T("function run() error(); end"));

         try
         {
            state.CallFunction(_T("run"));
         }
         catch (const Lua::Exception&)
         {
            // ok
            return;
         }
         catch (...)
         {
            Assert::Fail(_T("must throw Lua::Exception and nothing else"));
         }

         Assert::Fail(_T("must throw exception"));
      }

      /// tests RequireLib() method to load built-in libraries
      TEST_METHOD(TestStateRequireLib)
      {
         // setup
         Lua::State state;

         // run
         state.RequireLib("coroutine");

         // check
         Lua::Value value = state.GetValue(_T("coroutine"));
         Assert::IsTrue(value.GetType() == Lua::Value::typeTable, _T("type must be table"));

         Lua::Table table = state.GetTable(_T("coroutine"));
         Lua::Value funcYield = table.GetValue(_T("yield"));

         Assert::IsTrue(funcYield.GetType() == Lua::Value::typeFunction, _T("type must be function"));
      }

      /// tests temporary nil value handling
      TEST_METHOD(TestValueNilTemporary)
      {
         // set up
         Lua::State state;

         // run
         for (int i = 0; i < 100; i++)
         {
            Lua::Value value = state.GetValue("not_init");
            Assert::IsTrue(Lua::Value::typeNil == value.GetType(), _T("type must be nil"));
         }

         // check
         // must not overflow stack
      }

      /// tests CallFunction(), without return parameters
      TEST_METHOD(TestCallFunctionNoRet)
      {
         Lua::State state;

         state.LoadSourceString(_T("function run() return; end"));

         std::vector<Lua::Value> vecRetvals = state.CallFunction(_T("run"));
         Assert::AreEqual<size_t>(0, vecRetvals.size(), _T("must have returned zero return values"));
      }

      /// tests CallFunction(), with single return parameter
      TEST_METHOD(TestCallFunctionRetval)
      {
         Lua::State state;

         state.LoadSourceString(_T("function run() return 42; end"));

         std::vector<Lua::Value> vecRetvals = state.CallFunction(_T("run"), 1);
         Assert::AreEqual<size_t>(1, vecRetvals.size(), _T("must have returned one return value"));

         const Lua::Value& value = vecRetvals[0];
         Assert::IsTrue(Lua::Value::typeNumber == value.GetType(), _T("type must be number"));
         Assert::AreEqual(value.Get<double>(), 42.0, 1e-6, _T("value must be 42"));
      }

      /// tests CallFunction(), with multiple return parameters
      TEST_METHOD(TestCallFunctionMultipleRetvals)
      {
         Lua::State state;

         state.LoadSourceString(_T("function test() return 42, 42==6*7, \"123abc\", nil; end"));

         std::vector<Lua::Value> vecRetvals = state.CallFunction(_T("test"), 4);
         Assert::AreEqual<size_t>(4, vecRetvals.size(), _T("must have returned 4 return values"));

         Assert::IsTrue(Lua::Value::typeNumber == vecRetvals[0].GetType(), _T("type must be number"));
         Assert::IsTrue(Lua::Value::typeBoolean == vecRetvals[1].GetType(), _T("type must be boolean"));
         Assert::IsTrue(Lua::Value::typeString == vecRetvals[2].GetType(), _T("type must be string"));
         Assert::IsTrue(Lua::Value::typeNil == vecRetvals[3].GetType(), _T("type must be nil"));
      }

      TEST_METHOD(TestAddTable)
{'fraction_non_alphanumeric': 0.14696374971138304, 'fraction_numerical': 0.009120295543754329, 'mean_word_length': 2.5072874493927126, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1367943', 'n_tokens_mistral': 2559, 'n_tokens_neox': 2458, 'n_words': 551}