<commit_before>-- Tests for Synthesized Fields
-- ============================

-- These tests use `luassert` that exports various assertions.
local assert = require "luassert"

-- They also depend on the `synthesized` fields module, and the `tags`.
local synthesized = require "cosy.lang.view.synthesized"
local tags = require "cosy.lang.tags"

local VIEWS  = tags.VIEWS
local ERRORS = tags.ERRORS

-- For the tests, we define a tag (`TAG`) and a handler that computes a
-- value for this tag in the view.
local TAG  = tags.TAG
local function handler (data)
  data [TAG] = 0
end

-- Register handler
-- ----------------
--
-- The handler is then registered in the `synthesized` view.
assert.has.no.error (function () synthesized [TAG] = handler end)

-- Access to the synthesized field
-- -------------------------------
--
-- In the raw data, the field value is initially not set. After accessing
-- the field through the view, its value is set both in the view and in the
-- raw data.
do
  local data = {}
  assert.is_true (data [TAG] == nil)
  local view = synthesized (data)
  assert.are.equal (view [TAG], 0)
  assert.are.equal (data [TAG], 0)
end

-- A non synthesized field is writeable. A synthesized field generates an
-- error when it is written through the view (not in its handler, of
-- course).
do
  local data = {}
  local view = synthesized (data)
  assert.has.no.error (function ()
    view.something = 1
  end)
  assert.are.equal (view.something, 1)
  view [TAG] = 0
  assert.are.equal (# (data [ERRORS]), 1)
end

-- Views
-- -----
--
-- Synthesized views are stackable.
do
  local data = {}
  local view = synthesized (synthesized (data))
  assert.are.same (
    view [VIEWS],
    { synthesized, synthesized }
  )
  assert.are.equal (view [TAG], 0)
end<commit_msg>Update test for synthesized view after switching to view constructor.<commit_after>-- Tests for Synthesized Fields
-- ============================

-- These tests use `luassert` that exports various assertions.
local assert = require "luassert"

-- They also depend on the `synthesized` fields module, and the `tags`.
local synthesized = require "cosy.lang.view.synthesized"
local tags = require "cosy.lang.tags"

local ERRORS = tags.ERRORS

-- For the tests, we define a tag (`TAG`) and a handler that computes a
-- value for this tag in the view.
local TAG  = tags.TAG
local function handler (data)
  data [TAG] = 0
end

-- Register handler
-- ----------------
--
-- The handler is then registered in the `synthesized` view.
assert.has.no.error (function () synthesized [TAG] = handler end)

-- Access to the synthesized field
-- -------------------------------
--
-- In the raw data, the field value is initially not set. After accessing
-- the field through the view, its value is set both in the view and in the
-- raw data.
do
  local data = {}
  assert.is_true (data [TAG] == nil)
  local view = synthesized (data)
  assert.are.equal (view [TAG], 0)
  assert.are.equal (data [TAG], 0)
end

-- A non synthesized field is writeable. A synthesized field generates an
-- error when it is written through the view (not in its handler, of
-- course).
do
  local data = {}
  local view = synthesized (data)
  assert.has.no.error (function ()
    view.something = 1
  end)
  assert.are.equal (view.something, 1)
  view [TAG] = 0
  assert.are.equal (# (data [ERRORS]), 1)
end
{'fraction_non_alphanumeric': 0.15370866845397677, 'fraction_numerical': 0.004468275245755138, 'mean_word_length': 2.900116144018583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7473259', 'n_tokens_mistral': 1058, 'n_tokens_neox': 972, 'n_words': 418}