summaryrefslogtreecommitdiff
path: root/testsuite/test-syntax.c
blob: 6ee66f42be0611e90b902ab96bb4aac570d84743 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * This file is part of GtkSourceView
 *
 * Copyright 2021 Christian Hergert <chergert@redhat.com>
 *
 * GtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <gtksourceview/gtksource.h>
#include <gtksourceview/gtksourcebuffer-private.h>

static void
setup_search_paths (const char *basedir)
{
	GtkSourceLanguageManager *languages;
	GtkSourceStyleSchemeManager *styles;
	gchar *langs_path[2] = { NULL };
	gchar *styles_path;

  styles_path = g_build_filename (basedir, "data", "style", NULL);
	styles = gtk_source_style_scheme_manager_get_default ();
	gtk_source_style_scheme_manager_prepend_search_path (styles, styles_path);
  g_free (styles_path);

  langs_path[0] = g_build_filename (basedir, "data", "language-specs", NULL);
	languages = gtk_source_language_manager_get_default ();
	gtk_source_language_manager_set_search_path (languages, (const char * const *)langs_path);
  g_free (langs_path[0]);
}

static void
test_syntax (gconstpointer data)
{
  const char *filename = data;
  GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
  GtkSourceStyleSchemeManager *sm = gtk_source_style_scheme_manager_get_default ();
  GtkSourceStyleScheme *s = gtk_source_style_scheme_manager_get_scheme (sm, "Adwaita");
  GtkSourceLanguage *l;
  GtkSourceBuffer *buffer;
  GtkTextIter begin, end;
  char *content_type;
  GError *error = NULL;
  char *contents;
  gsize len;

  g_file_get_contents (filename, &contents, &len, &error);
  g_assert_no_error (error);
  g_assert_nonnull (contents);
  g_assert_cmpint (len, >, 0);

  g_print ("%s\n", filename);
  content_type = g_content_type_guess (filename, (const guint8 *)contents, len, NULL);
  l = gtk_source_language_manager_guess_language (lm, filename, content_type);

  if (l == NULL)
    {
      char *message = g_strdup_printf ("Skipping because cannot guess language for %s", filename);
      g_test_skip (message);
      g_free (message);
      goto cleanup;
    }

  buffer = gtk_source_buffer_new (NULL);
  gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), contents, len);
  gtk_source_buffer_set_language (buffer, l);
  gtk_source_buffer_set_highlight_syntax (buffer, TRUE);
  gtk_source_buffer_set_style_scheme (buffer, s);

  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &begin, &end);
  _gtk_source_buffer_update_syntax_highlight (buffer, &begin, &end, TRUE);

  g_object_unref (buffer);

  g_assert_nonnull (l);

cleanup:
  g_free (content_type);
  g_free (contents);
}

gint
main (gint   argc,
      gchar *argv[])
{
  const char *srcdir = g_getenv ("G_TEST_SRCDIR");
  char *path = g_build_filename (srcdir, "..", "tests", "syntax-highlighting", NULL);
  GDir *dir;
  GError *error = NULL;
  const char *name;

  g_assert_nonnull (srcdir);

	g_test_init (&argc, &argv, NULL);

  setup_search_paths (srcdir);

  dir = g_dir_open (path, 0, &error);
  g_assert_no_error (error);
  g_assert_nonnull (dir);

  while ((name = g_dir_read_name (dir)))
    {
      if (g_str_has_prefix (name, "file."))
        {
          char *testpath = g_strdup_printf ("/syntax-highlighting/%s", name);
          char *filename = g_build_filename (path, name, NULL);
          g_test_add_data_func_full (testpath, filename, test_syntax, g_free);
          g_free (testpath);
        }
    }

	return g_test_run ();
}